Hi there,
I'm fairly new to this, so please bare with me! :D I'm trying to either set the priority for a function, or temporiarly prevent another one from running. Basically I have 2 list boxes and when you select an option in one, it automatically selects the corresponding option in the other. When you press the remove button, I want it to remove the corresponding entries in both boxes. Unfortunately, when I hit remove, it removes it from the first box, but before running the very next line of code which would remove it from the second, it immediately runs the code of when the selected item changes. This causes an error because the boxes no longer match up with an item missing in one but not the other!
Here's my code:
private void btnRemove_Click(object sender, EventArgs e)
{
lstReplace.Items.Remove(lstReplace.SelectedItem);
lstFind.Items.Remove(lstFind.SelectedItem);
}
private void lstReplace_SelectedIndexChanged(object sender, EventArgs e)
{
lstFind.SelectedIndex = lstReplace.SelectedIndex;
}
private void lstFind_SelectedIndexChanged(object sender, EventArgs e)
{
lstReplace.SelectedIndex = lstFind.SelectedIndex;
}

Temporarily prevent a function from running
Hamp Turner
Youngermandl
a simple solution would be to include have a flag that says if you're removing an item and if you are then don't update the selected index.
private bool isRemoving = false;
private void btnRemove_Click(object sender, EventArgs e)
{
isRemoving = true;
lstReplace.Items.Remove(lstReplace.SelectedItem);
lstFind.Items.Remove(lstFind.SelectedItem);
isRemoving = false;
}
private void lstReplace_SelectedIndexChanged(object sender, EventArgs e)
{
if(!isRemoving)
lstFind.SelectedIndex = lstReplace.SelectedIndex;
}
private void lstFind_SelectedIndexChanged(object sender, EventArgs e)
{
if(!isRemoving)
lstReplace.SelectedIndex = lstFind.SelectedIndex;
}