|
Hello,
My code is trying to delete IExplorer history from the registry! The typed URLs are stored in a checked listbox, i can delete one value at a time both in the registry and from the checked listbox but when i try deleting multiple items at a time it doesn't work? here is the code
private void Delete_Click(object sender, EventArgs e)
{
RegistryKey key = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Internet Explorer\\TypedURLs", true);
string[] v = key.GetValueNames();
string[] t = new string[v.Length];
int count = checkedListBox1.Items.Count;
for (int i = 0; i < v.Length - 1; i++)
{
t[i] = (string)key.GetValue(v[i]);
if(t[i] == (string)checkedListBox1.SelectedItem)
{
key.DeleteValue(v[i]);
}
}
for ( int i = count; i > 0; i--)
{
if (checkedListBox1.CheckedItems.Contains(checkedListBox1.Items[i - 1]))
{
checkedListBox1.Items.RemoveAt(i - 1);
}
}
}
|
|
|
|
|
Hi,
if(t[i] == (string)checkedListBox1.SelectedItem)
SelectedItem is one item, use SelectedItems to get all selected items:
if (checkedListBox1.SelectedItems.Contains(t[i]))
Add also this in the constructor:
checkedListBox1.SelectionMode = SelectionMode.MultiExtended;
checkedListBox1.SelectionMode = SelectionMode.MultiSimple;
Hope this helps.
In some cases, my signature will be longer than my message...
<em style="color:red"> <b>ProgramFOX</b></em> ProgramFOX
modified 14-Jan-13 11:57am.
|
|
|
|
|
Still doesn't seem to have worked? Any other ideas please?
|
|
|
|
|
Erol2309 wrote: Any other ideas please?
I updated my answer.
In some cases, my signature will be longer than my message...
<em style="color:red"> <b>ProgramFOX</b></em> ProgramFOX
|
|
|
|
|
What does "doesn't work" mean? Ate you getting exceptions? What happens when you step over the code?
|
|
|
|
|
Well it means that when there are all the items in the checkedlistbox selected and i press the delete button they get deleted from the checkedlistbox but not from the registry...i cand only delete values from the registry one at a time and only when there are more than 2 entries..
|
|
|
|
|
I Checked your code for deleting the multiple items from the registry and it works fine, if doesnt work means check the checkbox comparison operations.
Also I doubt on the action performed inside the second for loop, the checkBox content is checked against its own value was it v[i] or t[i] arrays?
Jibesh V P
|
|
|
|
|
If you had stepped over your code, you would have seen what the problem is. Basically, you're iterating over one list of values, and you're getting one single value from the other list to compare it to. Try iterating over the selected items from the list box as well, and do your comparison in there.
Now, a more important point is that your comparison code is using terrible variable names. I ended up getting confused by your t and v variables.
|
|
|
|