I'm adding items from a List to a CheckedListBox. I want that box to display the friendly name of the item to the user but have a "secret" actual value to use when the user selects it.
foreach (string s in x)
{
checkedlistBox1.Items.Add(friendlyValue);
//Now how do I get this to have the real value?
}
With drop down menus I can set the DisplayName and ValueName to something and use something like:
combobox1.Items.Add(new ComboBoxItem { friendlyValue = x, realValue = y });
I can't seem to do this with a CheckedListBox.
Set the DisplayMember
and ValueMember
properties on the CheckedListBox
.
Those are functionally equivalent to the DisplayName
and ValueName
properties of the ComboBox
.
public class MyClass
{
public string FriendlyValue { get; set; }
public string RealValue { get; set; }
}
public class YourForm : Form
{
public YourForm()
{
var friendlyList
= new List<string>(); // imagine it's populated with friendly values
foreach (var fv in friendlyList)
{
checkedListBox1.Items.Add(
new MyClass { FriendlyValue = fv, RealValue = ??? });
}
checkedListBox1.DisplayMember = "FriendlyValue";
checkedListBox1.ValueMember = "RealValue";
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With