key-value in listbox
//set listbox
from https://stackoverflow.com/questions/8634066/cannot-do-key-value-in-listbox-in-c-sharp
var choices = new Dictionary<string, string>();
choices["A"] = "Arthur";
choices["F"] = "Ford";
choices["T"] = "Trillian";
choices["Z"] = "Zaphod";
listBox1.DataSource = new BindingSource(choices, null);
listBox1.DisplayMember = "Value";
listBox1.ValueMember = "Key";
//get item key value
from https://stackoverflow.com/questions/507354/how-do-i-get-at-the-listbox-items-key-in-c-sharp-winforms-app
KeyValuePair<string, string> kvp = (KeyValuePair<string, string>)listBoxEmp.SelectedItem;
string empID = kvp.Key;
//Remove ListBox item
listBoxEmp.DataSource = null;
listBoxEmp.Items.Clear();
留言
張貼留言