How to iterate DisplayMember and ValueMember in the ListBox

  • Thread starter Thread starter Jeff0803
  • Start date Start date
J

Jeff0803

Guest
Here is the code snippet which add items to the ListBox and set the DisplayMember and ValueMember.

List<ItemNameID> mylist = new List<ItemNameID>();

mylist.Add(new ItemNameID("Item1", "1"));
mylist.Add(new ItemNameID("Item2", "2"));
mylist.Add(new ItemNameID("Item3", "3"));
listBox1.DataSource = mylist;
listBox1.DisplayMember = "ItemName";
listBox1.ValueMember = "ItemID";





public class ItemNameID
{
private string _itemname;
private string _itemid;

public ItemNameID(string strItemName, string strItemID)
{

this._itemname = strItemName;
this._itemid = strItemID;
}

public string ItemName
{
get{return _itemname;}
set{_itemname = value;}
}

public string ItemID
{
get{return _itemid;}
set {_itemid = value;}
}
}



I can get the selected item's DisplayMember and ValueMember like following.

string display = listBoxLogo.Text;
string value = listBoxLogo.SelectedValue;

However I don't know how to iterate all items' DisplayMember and ValueMember.

I want to get like following result.

"Item1" "1"

"Item2" "2"

"Item3" "3"

How could I iterate those?

Continue reading...
 
Back
Top