tags:

views:

1163

answers:

3

I have a radio button on my Windows Form. How can I determine if the CheckChanged event occurred due to a user clicking or selecting the radio button vs programatically setting the event with

this.radioButtonAdd.Checked = true;

I would like some code to take a different action depending on if the user clicked the button or I raised the event myself.

Or maybe the better question is how do I handle the event when a user clicks vs when the state is changed in my code.

+2  A: 

If there is a built-in method, then I am not aware of it.

One way to handle it is by setting a global flag while the code is making changes that tells the event handler. For example, the code making the changes could look something like the following:

savedFlag = modifyingProgrammatically;
modifyingProgrammatically = true;
this.radioButtonAdd.Checked = true;
modifingProgrammatically = savedFlag;

Within your event handler:

if(modifyingProgramatically) {
    // The event was raised by an assignment within the code.
} else {
    // The event was raised by a user action.
}
Glomek
A: 

I think I found a pretty good answer.

All Windows Forms controls have a property called "Tag". its value can be any object.

So if I want to ingore any programatic changes I can do the following:

radioButton.Tag = "ignore"
radioButton.Checked = true

then in the event handler:

private void radioButton_CheckedChanged(object sender, EventArgs e)
{
    if (radioButton.Checked)
    {
        // Tag will be null in cases where the user clicks
        if (this.radioButtonAdd.Tag == null)
        {
            // do something
        }
        else
        { 
            // swallow action
            // reset Tag
            this.radioButtonAdd.Tag = null;
        }
    }
}
Omar Shahine
+1  A: 

You could also do something like changing a flag field value on MouseDown for the radio button, then reverting the flag value on MouseClick. Since the CheckedChanged event fires between the two, it can use the flag before it's reverted by the MouseClick event, and you don't have to worry about resetting its state.

You may still have threading issues to resolve if you're using a field as a flag (don't know much about the application you're working on), but something like this should work:

    private bool _mouseEvent;
    private void radioButton1_CheckedChanged(object sender, EventArgs e)
    {
        if (_mouseEvent)
            MessageBox.Show("Changed by mouse click.");
        else
            MessageBox.Show("Changed from code.");
    }

    private void radioButton1_MouseClick(object sender, MouseEventArgs e)
    {
        _mouseEvent = false;
    }

    private void radioButton1_MouseDown(object sender, MouseEventArgs e)
    {
        _mouseEvent = true;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        // This simulates a change from code vs. a change from
        // a mouse click.
        if (radioButton1.Checked)
            radioButton2.Checked = true;
        else
            radioButton1.Checked = true;
    }

Alternately, you could separate your "mouse-click-only" application logic so that it's triggered by a different event (like MouseClick).

Hope this helps.

Jared