Set values and displays into ComboBox Items
May 18th, 2009
1 comment
Small example of how we can load DisplayMember and ValueMember in ComboBox Items.
1. Create a class, which will contain objects that will put in the items.
public class AddDuration
{
private string duration_Display;
private long duration_Value;
public AddDuration(string Display, int Value)
{
duration_Display = Display;
duration_Value = Value;
}
public string Display
{
get { return duration_Display; }
}
public long Value
{
get { return duration_Value; }
}
}
2. Here the function to load data CoboBox.
private void FormActivator_Load(object sender, EventArgs e)
{
ArrayList durations = new ArrayList();
durations.Add(new AddDuration("1 година", 1));
durations.Add(new AddDuration("2 години", 2));
durations.Add(new AddDuration("3 години", 3));
durations.Add(new AddDuration("4 години", 4));
durations.Add(new AddDuration("5 години", 5));
this.cbDuration.DataSource = durations;
this.cbDuration.DisplayMember = "Display";
this.cbDuration.ValueMember = "Value";
}
Line 10 shows what string is displayed in the ComboBox.
Line 11 shows, which will return value from the ComboBox.
Unique visitors to post: 5