I have a listview that serves as a user-settings table. Each key is the setting, each subitem of the key is the value, and everything is nice and dandy.
I would like to be able to link this table of sorts to a checkbox. If the setting is true, the checkbox is checked, if it’s false, the checkbox isn’t checked. Let’s call the setting “settingChecked”.
The listview looks like:
| Setting | Value |
| settingChecked | true |
Something along those lines anyway… When the user inputs a setting, I have a function run to check the value of it and check the checkbox accordingly. Likewise, when they click the checkbox, I need to change the setting’s value in the table. Problem being that the settings aren’t case-sensitive. So if they put:
| Setting | Value |
| sEttIngChecked | true |
I wouldn’t be able to find the value with ListView1.Items[“settingChecked”]. The way I wrote it (I’m new to C#, so cut me some slack on this front) is to iterate through each element of the listview and check its lowercase text and the lowercase text of the setting. The code looks blocky and inefficient and doesn’t seem right. I could, of course correct the casing upon adding the setting, but I would like to let the users have the freedom to type as they please.
So, this arises two questions. First, how does the system find the member of a list by its key? Is there a reference of keys to their addresses for each list? Second, how do I find the element by its key, independent of case?
The best I can think of, in replace of the iteration, is a class with a Dictionary that references the item in the listview. This seems like it would also take up more resources than the program should.
Finally, how do I get rid of the annoying third-wheel of an extra column in the listview? Is the listview not meant to be used as a table? Is there a better tool to use?
edit: I’m aware I can do something like:
listView1.Items.Cast<ListViewItem>().Where(x => x.Text.ToLower() == "settingchecked").ToArray()[0];
but that seems like the same thing is a more concise form.
I’m not sure who, but someone had posted an answer not too long ago saying something about setting the key as lowercase, which made me realize that the text doesn’t have to be the same as the key. Though I had no idea how the key was set, I did a little testing and found out it’s the name of the listviewitem.
Anyway, something like: