RadioButton event _CheckedChanged running funtion twice [C#]

Shaitan00

Junior Contributor
Joined
Aug 11, 2003
Messages
358
Location
Hell
Okay - the way it works is I have 3 radio buttons and one datagrid, each time the user selects one of the radio buttons (because only one can be on at a time) I launch a function (RefreshGUI) that will reload the datagrid given the users new selection.
To determine when a radiobutton has been checked I am using the _CheckedChanged event - sadly this is having some negative effects...
So it goes something like this:
C#:
private void rbFINISHED_CheckedChanged(object sender, System.EventArgs e)
{
	RefreshGUI();
}

private void rbALL_CheckedChanged(object sender, System.EventArgs e)
{
       RefreshGUI();		
}

private void rbINPROGRESS_CheckedChanged(object sender, System.EventArgs e)
{
       RefreshGUI();		
}

private void RefreshGUI()
{
	... RefreshGUI
}

So the problem is, when rbFINSIHED is checked and the user goes to check rbALL instead this fires my RefreshGUI() function TWICE, once because rbFINSIHED is unchecking (change) and one because rbALL is being checked (change).
Obviously this greatly inneficient, I need a way so that I only end up firing my RefreshGUI() function ONCE as expected.
Any help/hints would be appreciated, thanks.
 
Last edited by a moderator:
I would create a single method similar to the one below and assign it to the CheckedChanged even of all of these checkboxes.

private void CheckChanged(object sender, System.EventArgs e)
{
if(((RadioButton)sender).Checked)
RefreshGUI();
}
 
the reason it does it twice is because even if it unchecks it is being changed so you get both things happening. Do like Cags suggested. or if you want to keep what you have. throw in an IF statement. I am not that familiar with C# but in VB I just put in the event

if RadioButton1.checked then
'code here
end if
 
Cags: GREAT idea - question is how do I call the "CheckChanged" function you wrote? I need it to launch when the user checks one of the boxes... I don't see your "CheckChanged" as an event?

techmanbd: That is what I am already doing, RefreshGUI is a set of 3 IF statements, one for each RadioButton - still the function is called twice.
 
Shaitan00 said:
techmanbd: That is what I am already doing, RefreshGUI is a set of 3 IF statements, one for each RadioButton - still the function is called twice.
RadioB becomes checked, causing RadioA to become unchecked. Now RadioA.CheckChanged is raised, but RadioB is checked, triggering a refresh (since you check all three). Then, RadioB.CheckChanged is raised, and it is checked, triggering a refresh. See?
 
Simply use the Event browser from the visual studio IDE and select that method, or add the following line to the constructor.

this.radioButton1.CheckedChanged += new System.EventHandler(this.CheckChanged);

NOTE: - Obviously you'll need to do the equivalent of that line for all radiobuttons, but using the event browser is a far easier option.
 
Last edited:
Back
Top