Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm Data Binding / June 2005

Tip: Looking for answers? Try searching our database.

databinding to picturebox.image

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Aurin - 16 Jun 2005 20:19 GMT
I have a windows form which contains many controls which display a single row
from a DataTable.  One of the controls is a picturebox.  I want to bind the
image property of the picturebox to a column in the DataTable such that:
If column value = “X”
    display image1
else
    display image2
The images themselves are NOT in the database.  They can be in an imagelist
or just files on the PC.

Can I do this in the designer properties databindings section or do I have
to write code?
"Jeffrey Tan[MSFT]" - 17 Jun 2005 08:32 GMT
Hi Aurin,

Thanks for your post.

Based on my understanding, you want to do simple databinding for PictureBox
with DataTable.

Databinding is no such smart to know where to retrieve the image to display
in the PictureBox, we should write code to tell it.
For example, assume the column we want to bind is of type int. We can
inherit from PictureBox and  create a new ImageIndex property and ImageList
property. Then we can bind ImageIndex property with the int type column, in
ImageIndex's set accessor, we can dynamically retrieve Image from ImageList
property with ImageIndex(assume we have assigned ImageList reference to our
PictureBox's ImageList property)

Sample code snippet like this:
public class MyPictureBox : System.Windows.Forms.PictureBox
{
    private ImageList il=null;
    public ImageList ImageList
    {
        get
        {
            return il;
        }
        set
        {
            il=value;
        }
    }

    private int _imageindex=-1;
    public int ImageIndex
    {
        get
        {
            return _imageindex;
        }
        set
        {
            _imageindex=value;
           
if(this.ImageList!=null&&this._imageindex>=0&&this._imageindex<this.ImageLis
t.Images.Count)
            {
                this.Image=this.ImageList.Images[this._imageindex];
            }
        }
    }
}
======================================================
Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Signature

Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Aurin - 20 Jun 2005 16:19 GMT
Thank you.  That helped.  by doing this in CurrencyManager.CurrentChanged
event, I have the correct picture diplaying.  
private void cm_CurrentChanged(object sender, EventArgs e)
{
    DataRow dr = ((DataRowView)cm.Current).Row;
    if (dr["arrow"].ToString() == "D")
        this.arrow.ImageIndex = 1;
    else
        this.arrow.ImageIndex = 0;
}

Is there any way that I can put this code in the DataBindings, advanced
section in the designer and therefore not have to write an event handler?  
Somewhere I thought that we could use expressions.  In short, how do you bind
to a property that does not match the datatype of the column?  In this
example I want to set an integer based on a column that is a string.  In
other cases I want to set the visible property (a bool) based on the value in
a string column.

> Hi Aurin,
>
[quoted text clipped - 57 lines]
> Get Secure! - www.microsoft.com/security
> This posting is provided "as is" with no warranties and confers no rights.
"Jeffrey Tan[MSFT]" - 21 Jun 2005 07:51 GMT
Hi Aurin,

Thanks for your feedback.

Sorry, I do not think I understand your point. In my last reply, I did not
use CurrencyManager.CurrentChanged, so I do not know what this event does.

It seems that you want to bind one type of datasource to another type of
property, yes? For this, we can use DataBinding.Parse and Format events to
do the type conversion, sample code like this:

DataTable dt=null;
private void Form1_Load(object sender, System.EventArgs e)
{
    dt=new DataTable();
    dt.Columns.Add("column1", typeof(int));
    dt.Columns.Add("column2", typeof(string));
    for(int i=0;i<5;i++)
    {
        DataRow dr=dt.NewRow();
        dr["column1"]=i;
        dr["column2"]=i.ToString();
        dt.Rows.Add(dr);
    }
      //We bind int type property to string type column
    this.myPictureBox1.DataBindings.Add("ImageIndex", dt, "column2");
    this.myPictureBox1.DataBindings[0].Format+=new
ConvertEventHandler(Form1_Format);
    this.myPictureBox1.DataBindings[0].Parse+=new
ConvertEventHandler(Form1_Parse);
}

//Convert column string data to int type and pass to the property
private void Form1_Format(object sender, ConvertEventArgs e)
{
    if(e.DesiredType==typeof(int))
    {
        e.Value=Int32.Parse((string)e.Value);
    }
}
//Parse property int data to string type and update the underlying
datasource
private void Form1_Parse(object sender, ConvertEventArgs e)
{
    if(e.DesiredType==typeof(string))
    {
        e.Value=e.Value.ToString();
    }
}

private void button1_Click(object sender, System.EventArgs e)
{
    this.myPictureBox1.DataBindings[0].BindingManagerBase.Position++;
}
=========================================================
Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Signature

Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Aurin - 21 Jun 2005 14:45 GMT
I'm sorry but I don't see how using format/parse will work.  Suppose I want
to do either of the following.  How would I code the parse event?
If string-value = D or S or M
  display image 0
else
  display image 1

or this:
if string-column is null
  visible = false
else
  visible = true

> Hi Aurin,
>
[quoted text clipped - 60 lines]
> Get Secure! - www.microsoft.com/security
> This posting is provided "as is" with no warranties and confers no rights.
"Jeffrey Tan[MSFT]" - 22 Jun 2005 03:09 GMT
Hi Aurin,

Thanks for your feedback.

For your first scenario, I do not think it is reasonable. DataBinding will
not allow several datasource value to be mapped into single UI content.
This is because Winform databinding is 2 ways. We can map "D" or "M" or "S"
to 0 without any problem, however, how can we map 0 or 1 property value
back to datasource? Should we map 0 property value back to "D" or "M" or
"S"? We can not determine this, so this is not a reasonable scenario for
databinding. We should use databinding for 1-to-1 mapping.

Hope this helps
====================================
Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Signature

Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

"Jeffrey Tan[MSFT]" - 24 Jun 2005 06:31 GMT
Hi Aurin,

Does my reply make sense to you? If you still have any concern, please feel
free to tell me, thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Signature

Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.