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; } }
}
...
- Jamie
Otis Mukinfus - 28 Oct 2006 18:38 GMT
>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.
[quoted text clipped - 65 lines]
>...
>- Jamie
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
http://www.tomchilders.com
Jamie Risk - 29 Oct 2006 03:10 GMT
"Otis Mukinfus" <phony@emailaddress.com> wrote in message
> >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.
[quoted text clipped - 3 lines]
> >
> >I'd really appreciate a more streamlined method to implement this.
...
> >- Jamie
>
[quoted text clipped - 3 lines]
>
> Otis Mukinfus
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