Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

RadioButton Event Handling... Is There a Better Way?

Status
Not open for further replies.

BoulderBum

Programmer
Jul 11, 2002
2,179
US
I have a group of RadioButtons in my Windows Form that has a CheckedChanged event handler attached. The event handler causes an update for a selected ListBox item. The problem? The ListBox has an event handler that sets the selected radio button, and the CheckedChanged event fires when that happens.

I am considering detaching the RadioButton's event handler temporarily while its value is being set, but that seems inefficient.

Is there a better approach?
 
Hackish solution, but it may give you some ideas:

//Create some flag
private int IgnoreListBoxChanges;

// Here is the Radio Button controller
... RadioButtonEventHandler(...)
{
IgnoreListBoxChanges = 1;
...Update List Box...
IgnoreListBoxChanges = 0;
}

// Here is the List Box Controller
... ListBoxEventHandler(...)
{
if(IgnoreListBoxChanges == 0)
{
...Do event handler stuff
}
}

As I said, this is a very hackish way to do this, but it does work. Of course, this trusts that you can manage that flag correctly and not accidentally set it. Basically, if you select the Radio Button, it sets the flag to ignore whatever changes happen in the List Box. The List Box is updated, and the List Box event handler is still called. However, it checks to see that the flag has been set to ignore these changes, and the event handler does nothing. When the Radio Button event handler finishes, the flag is set back to 0. If the List Box is selected at this point, the event handler will operate as specified. I’m sure someone out there has a better solution then this, but this could get you by in a pinch or at least give you some ideas.

-Andrew R.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top