简体   繁体   English

单选按钮CheckedChanged事件仅在选中时?

[英]Radio button CheckedChanged event only when checked?

Situation 情况

I want to have an event for when a radiobutton is checked only. 我只想当单选按钮被选中时有一个事件。

I know that the event CheckedChanged event is called when a radio button is checked and unchecked and I know why. 我知道选中和取消选中单选按钮时会调用事件CheckedChanged事件,我知道原因。 I know how to use it to only have code execution when it is checked. 我知道如何使用它仅在选中时执行代码。 See : Radio button checked changed event fires twice 请参阅: 单选按钮选中的更改事件触发两次

Question

However I worked with others languages which had event for when the radio button is checked (and not called when unchecked). 但是我使用了其他语言,这些语言在单选按钮被选中时会发生事件(未选中时不会被调用)。 Is there such an event that directly do this in C# 是否有这样的事件可以直接在C#中执行

(it's mostly a yes or no question to know if there is a specific event for this, since I'm well aware I can make a check in the event) ? (要知道是否有特定的事件,通常是一个是或否的问题,因为我知道我可以检查该事件)

Not to my knowledge. 据我所知。 You'd be best off doing something like this: 您最好这样做:

var radioButton = sender as RadioButton;
if (radioButton == null || !radioButton.Checked) return;
// your code ...

You can change the event handler of the Radiobutton inside your page events when page is posted back. 当页面回发时,您可以在页面事件中更改单选按钮的事件处理程序。 For this, you need to use the Radiobutton value from VIEWSTATE and then assign event handler. 为此,您需要使用VIEWSTATE中的Radiobutton值,然后分配事件处理程序。

One generic work-around is a to use a listbox styled as radio buttons to prevent having to use multiple bindings/controls/checking on checked. 一种通用的解决方法是使用样式为单选按钮的列表框,以防止必须使用多个绑定/控件/选中复选框。 There's only one control which event needs to be monitored ( SelectedIndexChanged ) and instead of checking which radiobutton is checked there's SelectedItem . 只有一个控件需要监视哪个事件( SelectedIndexChanged ),而不是检查选中了哪个单选按钮,而是使用SelectedItem Another advantage is the ability to bind datasources instead of dynamically adding radiobuttons. 另一个优点是能够绑定数据源,而不是动态添加单选按钮。

For that purpose, here's a simple RadioButtonBox control that does just that (made it a long time ago and haven't revised it since, but still use it regularly) 为此,这里有一个简单的RadioButtonBox控件,它就是这样做的(很久以前就做了,从那以后就没有修改过,但仍然定期使用)

public class RadioButtonBox:ListBox
{
    public readonly RadioButtonBoxPainter Painter;

    public RadioButtonBox()
    {
        Painter = new RadioButtonBoxPainter(this);
    }

    [DefaultValue(DrawMode.OwnerDrawFixed)]
    public override DrawMode DrawMode
    {
        get{return base.DrawMode;}
        set{base.DrawMode = value;}
    }
}

public class RadioButtonBoxPainter : IDisposable
{

    public readonly ListBox ListBox;
    public RadioButtonBoxPainter(ListBox ListBox)
    {
        this.ListBox = ListBox;
        ListBox.DrawMode = DrawMode.OwnerDrawFixed;
        ListBox.DrawItem += ListBox_DrawItem;
    }

    void ListBox_DrawItem(object sender, DrawItemEventArgs e)
    {
        if (e.Index == -1) return;
        Rectangle r = e.Bounds;
        r.Width = r.Height;
        bool selected = (e.State & DrawItemState.Selected) > 0;
        e.DrawBackground();
        e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
        ControlPaint.DrawRadioButton(e.Graphics, r, selected ? ButtonState.Checked : ButtonState.Normal);
        r.X = r.Right + 2;
        r.Width = e.Bounds.Width - r.X;
        string txt;
        if (ListBox.Site != null && ListBox.Site.DesignMode && e.Index >= ListBox.Items.Count)
            txt = ListBox.Name;
        else
            txt = ListBox.GetItemText(ListBox.Items[e.Index]);
        using (var b = new SolidBrush(e.ForeColor))
            e.Graphics.DrawString(txt, e.Font, b, r);
        if (selected)
        {
            r = e.Bounds;
            r.Width--; r.Height--;
            e.Graphics.DrawRectangle(Pens.DarkBlue, r);
        }
    }

    public void Dispose()
    {
        ListBox.DrawItem -= ListBox_DrawItem;
    }
}

The RadioButtonBox is the control, RadioButtonBoxPainter is the class that does the custom painting and can be used on any ListBox (the painting can be integrated into the control as well, but at the start this was made to give some existing listboxes a radiobutton look). RadioButtonBox是控件, RadioButtonBoxPainter是执行自定义绘画的类,可以在任何ListBox上使用(绘画也可以集成到控件中,但是一开始它是为了使某些现有列表框具有单选按钮外观) 。

As for the display, normally it would still have that listbox feel: 至于显示,通常它仍然具有该列表框的感觉:

单选按钮框

But by setting backcolor to 'Control' and no borderstyle, the result looks like regular radiobuttons: 但是通过将backcolor设置为“ Control”并且没有边框样式,结果看起来像常规单选按钮:

RadioButtonBox2

If the second layout is always preferred, they can always be set as defaults in the RadioButtonBox control. 如果总是首选第二种布局,则可以始终在RadioButtonBox控件中将它们设置为默认设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM