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 / October 2006

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jamie Risk - 27 Oct 2006 18:50 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.

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

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.