Combo Box

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

hi
Currently I am working in C# windows application. I am trying to fill combo box with values
We have to always use datasource property for filling values in Combo box?
In VB, normally i have used Item data property and display data property
We can't use this similar method in C#?? please let me know.

thankjs
Kannan
 
it's very easy - ex :

ComboBox cb = new ComboBox();
string s = "hi";
cb.Items.Add( s );
// or
cb.Items.AddRange( anObjectArray );

where anObjectArray is an array of System.Object based objects such as
strings etc.

;-)
 
I usually use a helper class that overrides the ToString() method. Any class
that overrides the ToString() method with a "useful" string will work.

public class ComboBoxItem
{
private string display;
private object data;

public ComboBoxItem(string display, object data)
{
this.display = display;
this.data = data;
}

public override string ToString()
{
return display;
}
}

You'd add it to your ComboBox like this:

comboBox.Items.Add(new ComboBoxItem("asdf", yourObjectHere));
 
you do this at design time through the Properties page in the Items property

click the property and a [...] button appears on the right of the field -
click it and you get a dialog box that allows you fill in the items in the
list box

these can be fully edited later in code if you need to

8-)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top