Is there any way to suspend the trigger of a particular event?

B

babylon

I have 2 ComboBoxes,
the selectedIndexChanged event handler in ComboBox A change the
SelectedIndex of ComboBox B
the selectedIndexChanged event handler in ComboBox B change the
SelectedIndex of ComboBox A

so whenever I change anyone of them, they would trigger the
SelectedIndexChanged event of other ComboBox continuously...

is there a way to suspend it?
so that I could If I change combobox A, B will changed and vice versa +
without circular triggering...

thank you
 
M

Morten Wennevik

Hi babylon,

Yes, it is possible to "suspend" the event handler, though I'm not sure
how to do it in Visual Basic.

In C# you would add an EventHandler with +=, so you can remove it with-=.

// unsubscribe the event
ComboBoxA.SelectedIndexChanged -= new EventHandler(SomeMethod);

// do some stuff

// and resubscribe
ComboBoxA.SelectedIndexChanged += new EventHandler(SomeMethod);


Happy coding!
Morten Wennevik [C# MVP]
 
B

babylon

Hi
the EventHandler is registered by external components which is not know in
the class that contains the 2 comboboxes

As I can't really 'save' all the registered handlers without holding the
reference of the original EventHandler
I couldn't remove the EventHandler before I change the
ComboBox.SelectedIndex value

thank you very much anyway

Hi babylon,

Yes, it is possible to "suspend" the event handler, though I'm not sure
how to do it in Visual Basic.

In C# you would add an EventHandler with +=, so you can remove it with -=.

// unsubscribe the event
ComboBoxA.SelectedIndexChanged -= new EventHandler(SomeMethod);

// do some stuff

// and resubscribe
ComboBoxA.SelectedIndexChanged += new EventHandler(SomeMethod);


Happy coding!
Morten Wennevik [C# MVP]
 
G

Guest

you can suppress it via little coding as follows (just a blue-print ofcourse):

private boolean changetAtA'true if comboA is used
private boolean changetAtB 'true if comboB is use

private sub comboA_selectedindexchange(............................
If changeAtB The
changeAtB=fals
exit su
els
changeAtA=tru
changeAtB=false;
comboB.selectedindex=comboB.FindStringExact("some string value"
End I
end su

private sub comboB_selectedindexchange(..............................
If changeAtA The
changeAtA=fals
exit su
els
changeAtB=tru
changeAtA=fals
comboA.selectedindex=comboA.FindStringExact("some string value"
End I
end sub
 
M

Morten Wennevik

What if both ComboBoxes uses the same event?
You could determine which one using the sender object and set the other
combobox values without triggering... oh wait, that still would trigger it
I suppose.

I guess you need to use a flag somewhere. Set it to true or false at the
first trigger, and set it to the opposite in the next one. You would
probably need separate flags for each box and have both events check the
other ones flag.

Event1
{
if(Event2Triggered)
{
Event2Triggered = false;
return;
}
}

Event2
{
if(Event1Triggered)
{
Event1Triggered = false;
return;
}
}

Just thinking out loud, maybe it can give you some ideas :)

Happy coding!
Morten Wennevik [C# MVP]
 
B

babylon

thx for you guys help

Since the methods you mentioned needes explicit handling in the
SelectedIndexChanged Event handler..
What I wanna to do is to write a class library for others to use...and I
don't want the one who use the library needes to handle the problem in the
SelectedIndexChanged Event Handler they provided.

thx


What if both ComboBoxes uses the same event?
You could determine which one using the sender object and set the other
combobox values without triggering... oh wait, that still would trigger it
I suppose.

I guess you need to use a flag somewhere. Set it to true or false at the
first trigger, and set it to the opposite in the next one. You would
probably need separate flags for each box and have both events check the
other ones flag.

Event1
{
if(Event2Triggered)
{
Event2Triggered = false;
return;
}
}

Event2
{
if(Event1Triggered)
{
Event1Triggered = false;
return;
}
}

Just thinking out loud, maybe it can give you some ideas :)

Happy coding!
Morten Wennevik [C# MVP]
 
H

Herfried K. Wagner [MVP]

* "babylon said:
the selectedIndexChanged event handler in ComboBox A change the
SelectedIndex of ComboBox B
the selectedIndexChanged event handler in ComboBox B change the
SelectedIndex of ComboBox A

so whenever I change anyone of them, they would trigger the
SelectedIndexChanged event of other ComboBox continuously...

is there a way to suspend it?
so that I could If I change combobox A, B will changed and vice versa +
without circular triggering...

Use a Boolean variable that tells the other procedure not to process
anything.

Alternatively, you can remove the event handler using 'RemoveHandler'
and add it afterwards using 'AddHandler'.
 
B

babylon

thank you!

Herfried K. Wagner said:
Use a Boolean variable that tells the other procedure not to process
anything.

Alternatively, you can remove the event handler using 'RemoveHandler'
and add it afterwards using 'AddHandler'.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top