tags:

views:

53

answers:

5

I'm new to C# and Windows Form but if I have a radiobutton and I call radiobutton1.Checked=true, is there a way for it to not fire the CheckedChange event? I want to distinguish between the user clicking on the radiobutton and me setting the radiobutton programmatically. Is this possible?

A: 

Why should your code care who checked the radiobutton?

EDIT: There are ways around this (subclass, flag), but don't. The only "legit" reason I can think of for wanting this is to prevent some side-effect from happening when the value is initially (programatically) displayed, and even that is suspect. Rethink the side-effect, does it really belong on the change-event, or the commit?

More info one why/what would help. On the surface, this looks like a design error.

Steven A. Lowe
I think he wants to do different things depending on users clicking and code-behind changing.
Danny Chen
Youa are right.
genomess
i understand that is what the OP wants; i do not understand why, and suspect a design error in the need
Steven A. Lowe
A: 

One (hackish) way to do it would be to subclass RadioButton and override the OnCheckChanged virtual method, suppressing the event if the Checked property has been set programmatically.

However, since radio-buttons belong to a group, the event always fires in pairs (oen for the uncheck, one for the check). You will therefore want to suppress the event for the entire group when you choose the selected button programmatically. Here's an example implementation:

public class CustomRadioButton : RadioButton
{
    private bool _suppressCheckedEvent;

    public void SetChecked(bool value, bool suppressCheckedEvent)
    {
        if (!suppressCheckedEvent)
            Checked = value;
        else
        {
            SetSupressModeForGroup(true);
            Checked = value;
            SetSupressModeForGroup(false);
        }
    }

    private void SetSupressModeForGroup(bool suppressCheckedEvent)
    {
        foreach (var crb in Parent.Controls.OfType<CustomRadioButton>())
            crb._suppressCheckedEvent = suppressCheckedEvent;
    }

    protected override void OnCheckedChanged(EventArgs e)
    {
        if (!_suppressCheckedEvent)
            base.OnCheckedChanged(e);
    }   
}

In this implementation, changing the checked-state through the Checked property will always fire the event. When you call the SetChecked method, you have the choice to suppress the event.

Ani
+2  A: 

Here's a straightforward method of using the event when you feel like it.

private bool SuppressRadioButton1Event { get; set; }

private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
    if (!this.SuppressRadioButton1Event)
    {
        MessageBox.Show("Not suppressed!");
    }
}

private void button1_Click(object sender, EventArgs e)
{
    this.SetRadioButton1(false);
}

private void SetRadioButton1(bool checkedOn)
{
    this.SuppressRadioButton1Event = true;
    radioButton1.Checked = checkedOn;
    this.SuppressRadioButton1Event = false;
}
Anthony Pegram
+1 I have to admit this way of `flagging` is better than mine :)
Danny Chen
+1  A: 

A very easy way:

public void radio_OnCheckChanged(object sender, EventArgs e)
{
   RadioButton r = sender as RadioButton;
   bool isUserChange = r.Tag.Equals(1);
   if (isUserChange) blabla
   else blabla
   r.Tag = null;       
}

public void MyMethod()
{
   radio1.Tag = 1;
   radio.Checked = true;
}

You can use any kind of flag which users can't do by their clicking.But you can do via your code.

Danny Chen
A: 

Stop trying to defeat the design of the CheckedChanged event. It's specifically supposed to include programmatic changes.

If you want user-triggered changes and not programmatic changes, use the Click event instead. (You may be thinking that you don't want to restrict yourself to mouse clicks, don't worry, there's a MouseClick event for that, Click includes keyboard changes as well.)

Ben Voigt
@Ben Voigt: This does not fire for a `RadioButton` when it goes from a checked to an unchecked state.
Ani
@Ani: genomess said he wanted an event to fire only when the user clicks the button. I'm pretty sure that no radio button ever becomes unchecked by the user clicking on it.
Ben Voigt
@Ben Voigt: Right, fair enough.
Ani