Display Friendly Name in CheckedListBox
Asked Answered
M

1

6

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.

Munitions answered 13/3, 2014 at 2:40 Comment(1)
Right WindowsForms sorry. I'm a Systems Engineer not so much a developer but I am making apps for active directory stuff. In this case my List<string> is populated by an active directory search.Munitions
M
5

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";        
    }
}
Madisonmadlen answered 13/3, 2014 at 2:45 Comment(6)
This does not appear to be available as an option. If I type checkedListBox1.DisplayMember in VisualStudio, nothing comes up. It isn't in the list. Also, I can't seem to add anything called "CheckedListBoxItem" to checkedListBox1.Items.Add. That isn't something that exists as a namespace. There's no such thing as CheckedListBoxItem as far as my copy of 2010 thinks.Munitions
@Sephethus For some reason microsoft hide those properties from intellisense. but when you set it will work :)Boothe
I tried setting the two properties without intellisense and it didn't give me an error, so that part works. What about adding these values to the list box? I tried using similar code to my comboboxitem example above except I used CheckedListBoxItem and it gives an error saying that doesn't exist in the namespace.Munitions
Really confused at this point. I set the properties there, but how do I add them to the checkedlistbox as separate values? Normally I go clb.Items.Add(x); if it is just a single value, and if it's two values I should be able to use clb.Items.Add(new CheckedListBoxItem{ FriendlyValue = x, RealValue = y}); Unfortunately CheckedListBoxItem does not exist, while ComboBoxItem and ListBoxItem do exist.Munitions
The list of values is coming from an active directory search. The actual list is populated with the real value, the friendly value is just a part of the full value. I'm removing the fqdn and backslashes to make it readable by the user. Strange thing is when I try it the way you edited it actually ends up making it try to pass "MyClass" as part of the command in the event, rather than the RealValue.Munitions
I just figured it out! I realized I wasn't passing the real value in the event. I had to set MyClass mc = (MyClass)CheckedListBox1.SelectedItem; and then pass mc.RealValue. Thanks for the help!Munitions

© 2022 - 2024 — McMap. All rights reserved.