Basically the windows combobox is slightly different from the Webforms's Dropdownlist.
Web dropdown provides a ListItem class to store Text and Value for the dropdown.
To add Text and Value parts to a Windows combobox do this,
Create a new class and name it myListItem and add two properties to it name Text and Value.
Follow the code below to get it working......
Code to populate the combobox.
=======================
comboBox1.Items.Add(new myListItem("1","11"));
comboBox1.Items.Add(new myListItem("2","22"));
comboBox1.Items.Add(new myListItem("3","33"));
comboBox1.Items.Add(new myListItem("4","44"));
comboBox1.Items.Add(new myListItem("5","55"));
comboBox1.DisplayMember = "Text"; //should refer the property defined in the class
comboBox1.ValueMember = "Value"; //should refer the property defined in the class
Code to Read selected items from combobox.
================================
((myListItem)comboBox1.SelectedItem).Value;
myListItem Class Code block.
=====================
public class myListItem
{
private string _Text;
private string _Value;
public myListItem(string sText, string sValue)
{
Text = sText;
Value = sValue;
}
public string Text
{
get
{
return _Text;
}
set
{
_Text = value;
}
}
public string Value
{
get
{
return _Value;
}
set
{
_Value = value;
}
}
}
Anto Rocked!
13 years ago
No comments:
Post a Comment