赢表格,选中后如何为单选按钮分配方法

时间:2019-05-30 16:34:24

标签: c# winforms

我是c#和Win表格的新手,所以请耐心等待我。

我有e个以编程方式编写的单选按钮。我想更新选中​​哪个按钮的值。这是其中一个按钮的代码

        RadioButton cab = new RadioButton();
        cab.Text = m_appContext.L10nManager.TranslateString("Modern");
        cab.Name = "Modern";
        cab.Location = new Point(15, 45);
        cab.AutoSize = true;

我想在检查时分配一个“方法”。所以我继续将其添加到我的单选按钮中:

ca.Checked = m_appContext.EWSMode;

这是EWSMode的代码:

    public enum EWSMode { Auto = 0, EWS = 1, Classical = 2 };
    EWSMode m_EWSMode = EWSMode.Auto;
    public EWSMode EWSMode
    {
        get { return m_EWSMode; }
        set { m_EWSMode = value; }
    }

取决于选择哪个单选按钮,我要将 m_EWSMode 设置为该值。因此,默认情况下

EWSMode m_EWSMode = EWSMode.Auto;

说说单选按钮EWS时,我要将 m_EWSMode 设置为EWS

我什么时候将新值设置/传递给EWSMode枚举,我该怎么做? enter image description here

1 个答案:

答案 0 :(得分:1)

您必须使用EventHandler,如下所示:

ca.Checked += new System.EventHandler(EWSMode);

请注意,EWSMode必须具有正确的签名,并以objectEventArgs作为参数:

private void EWSMode(object sender, EventArgs e)

如果要根据选中的检查来设置变量的值,可以将它们都订阅到事件处理程序中:

ca1.Checked += new System.EventHandler(EWSMode_Event);
ca2.Checked += new System.EventHandler(EWSMode_Event);

然后将逻辑放在EWSMode_Event中:

private void EWSMode_Event(object sender, EventArgs e)
{
//When ca1 or ca2 gets checked, this event will be triggered. Here, you would
//set your m_EWSMode variable.
}

您可以对每个CheckBox使用一个事件来做相同的事情:

ca1.Checked += new System.EventHandler(EWSMode_Event);

private void EWSMode_Event(object sender, EventArgs e)
{
    //When ca1 gets checked, this event will be triggered.

    EWSMode m_EWSMode = EWSMode.EWS;
}