novice help for combo box needed

  • Thread starter Thread starter David C
  • Start date Start date
D

David C

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.
 
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.




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.
 
Alternatively, if there is a type already and the ToString doesn't return
desired text, one can use DisplayMember and ValueMemeber properties.

If you use .NET 2.0 you can use the FormatString property declared on the
level of the ListControl (base class for the combobox)

combobox.Items.Add(10);
combobox.Items.Add(15);
combobox.Items.Add(12);
combobox.FormatString = "# 'Days'";

the combobox items will display :
10 Days
15 Days
12 Days

but the values will be the integers 10, 12 ,12


--
HTH
Stoitcho Goutsev (100)

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.




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.
 
Back
Top