Archive

Archive for the ‘.NET’ Category

C# Adding an Object to a ComboBox

October 26th, 2007

I needed a control similar to the HTML <SELECT> menu where you can have a key - value pair. To do this in C#, you first create an object like the one below: Notice the “ToString” method, that is what will display in the dropdown.

public class area
{
public string areaName = “”;
public int aID;

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

Next, create your object and add it to your comboBox

area theArea = new area();
theArea.aID = areaID;
theArea.areaName = areaName;
comboAreas.Items.Add(theArea);

admin .NET