all groups > dotnet windows forms databinding > october 2006 >
You're in the

dotnet windows forms databinding

group:

Is there a better way to bind a listbox (combBox)?


Is there a better way to bind a listbox (combBox)? Jamie Risk
10/27/2006 1:50:01 PM
dotnet windows forms databinding:
I'd like to bind a combo box control to an object. i.e. bind an
index number for an array of display strings to my data object.

I've got something that works as far as I've tested it, but it
has to be an overly convoluted method.

I'd really appreciate a more streamlined method to implement this.


My Code:

....
myComboBox.DataSource = myData.ColorDisplay;
myComboBox.DisplayMember = "Txt";
myComboBox.ValueMember = "ID";
....

I then was able to bind the control to my object.

....
myComboBox.DataBindings.Add(new
System.Windows.Forms.Binding("SelectedValue",
this.myDataBindingSource,"ColorValue",true));
....

The class:

....

public class myData
{
public enum myColor_enum { black, white, red };

public class myColors
{
// Data
private myColor_enum iD;
private string txt;

// Methods
public myColor_enum ID
{ get { return iD; } set { iD = value; } }

public string Txt
{ get { return txt; } set { txt = value } }

// Constructor
public myColors(myColor_enum colorID, string text)
{ this.Txt(text); this.ID(colorID); }
}

// Data
private myColors[] colorDisplay =
{
new myColors(myColor_enum.black,"Black"),
new myColors(myColor_enum.white,"White"),
new myColors(myColor_enum.red,"Red")
}
private myColor_enum colorValue;

// Methods
public myColor_enum ColorValue
{ get { return colorValue; } set { colorValue = value; } }
public myColors[] ColorDisplay
{ get { return colorDisplay; }
set { colorDisplay = value; } }
}


....
Re: Is there a better way to bind a listbox (combBox)? Otis Mukinfus
10/28/2006 12:38:47 PM
[quoted text, click to view]

You're almost there. Now put the objects into A BindingList<T> object and us it
for the data source.
Good luck with your project,

Otis Mukinfus
http://www.arltex.com
Re: Is there a better way to bind a listbox (combBox)? Jamie Risk
10/28/2006 10:10:39 PM

[quoted text, click to view]

....

[quoted text, click to view]

Thanks for responding, although I was hoping my solution would be considered
a poor implementation at best implying that there was, in fact, a better
way.

- Jamie

AddThis Social Bookmark Button