I have a problem, that I can’t solve.
I am writing an application that will help to change system proxy easily. It has a listView with some items. These items have checkboxes.
Logic of application demands that only one item can be checked at a time, so I have following code to make sure it works well:
private void listView1_ItemChecked(object sender, ItemCheckedEventArgs e)
{
listView1.ItemChecked -= listView1_ItemChecked;
foreach(ListViewItem item in listView1.Items)
{
if(item != e.Item)
{
item.Checked = false;
}
}
listView1.ItemChecked += listView1_ItemChecked;
}
Also my application needs to check some value in registry on it’s start and compare it’s text with my items in listView.
I am doing it like that:
private void GetProxyFromRegistry()
{
RegistryKey SystemProxy = Registry.CurrentUser.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Internet Settings");
try
{
string UsedProxy = SystemProxy.GetValue("ProxyServer").ToString();
foreach (ListViewItem item in listView1.Items)
{
if (UsedProxy == item.Text + ":" + item.SubItems[1].Text)
{
item.Checked = true;
}
else
{
item.Checked = false;
}
}
}
catch (Exception exc)
{
MessageBox.Show(exc.Message);
}
}
The problem is that none of the listView1.Items will be checked except last one when used.
I know that it’s because of listView1_ItemChecked(), but I do not know how to solve it the other way.
Can you help me find a solution how to either uncheck all other checkboxes, or find a workaround to make correct item be checked?
So starting with your ItemCheck event, it should be like this:
For the next issue, I’d suggest setting a breakpoint at this line of code here:
When the breakpoint is hit (on the last index), check to see what’s different about that item in comparison to the others (look at case sensitivity, for instance).