newbie help for winform combo box needed

  • Thread starter Thread starter David Cho
  • Start date Start date
D

David Cho

I am trying to emulate with ComboBox, what I do with a Web DropDownList.

dropdownList.Items.Add(new ListItem("30", "30 days"));
dropdownList.Items.Add(new ListItem("60", "60 days"));

As you can see, 30 and 60 are values and "30 days" and "60 days" for
displaying. Not populating from the database here. I just want those
two items to be in the combo box. Very simple.

Cannot for the life of me figure out how to do this. ComboBox.Items is
an ObjectCollection, and I have no idea how to emulate the above.

So I decided to create table like this and populate.

DataTable table = new Table();

table.Columns.Add("Value");
table.Columns.Add("Display");

//and create rows with what I want to populate
DataRow row = table.NewRow();
row["Value"] = 30;
row["Display"] = "30 days";
table.Rows.Add(row);

cmbBox.ValueMember = "Value";
cmbBox.DisplayMember = "Display";
cmbBox.DataSource = table;

Now that seems like a lot of code for something very simple.

And the strangest thing is, when cmbBox.DataSource = table executes, the
SelectedIndexChanged event fires!

PS: I am still using .NET 1.x.
 
Thanks.

One problem is I post via Developersdex.com during the day, so I have no
access to the Winforms group. Will check out your answer.
 
Allright,

I'll just copy/paste the Winforms answer here, in case you haven't had an
opportunity to access the group.

In addition, the answer Stoitcho Guitsev provided would work very well,
though you would need .net 2.0 for that (pasted his letter at the bottom).

===

Hi David,

You can create your own ListItem class or just put "30" and override the
drawing algorithm to display the item text + " days".

The first way can be done like this

ComboBox b = new ComboBox();
b.Items.Add(new ComboItem("30", "30 days"));
b.Items.Add(new ComboItem("60", "60 days"));
this.Controls.Add(b);


class ComboItem
{
public string value1;
public string value2;
public ComboItem(string value1, string value2)
{
this.value1 = value1;
this.value2 = value2;
}
public override string ToString()
{
return value2;
}
}

The ComboBox will call ToString to determine what to display.

Another way to do the above can give you more flexibility in what to
display

ComboBox b = new ComboBox();
b.Items.Add(new ComboItem("30", "30 days"));
b.Items.Add(new ComboItem("60", "60 days"));
b.DisplayMember = "Display";
this.Controls.Add(b);

class ComboItem
{
private string value;
private string display;
public string Value
{
get { return value; }
}
public string Display
{
get { return display; }
}
public ComboItem(string value1, string value2)
{
this.value = value1;
this.display = value2;
}
}

Note the DisplayMember usage. The ComboBox can display any string
property by setting the DisplayMember to the name of the property.
 

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