Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何触发单选按钮';以编程方式创建事件处理程序?_C#_.net_Visual Studio 2010_Event Handling_Radio Button - Fatal编程技术网

C# 如何触发单选按钮';以编程方式创建事件处理程序?

C# 如何触发单选按钮';以编程方式创建事件处理程序?,c#,.net,visual-studio-2010,event-handling,radio-button,C#,.net,Visual Studio 2010,Event Handling,Radio Button,请看一下下面的代码 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace calc { public partial class Form1 : Form { //cod

请看一下下面的代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace calc
{
    public partial class Form1 : Form
    {
        //code removed

        public Form1()
        {

            InitializeComponent();
            calculator = new Calculator();

            maleRadio.PerformClick();
            englishRadio.PerformClick();
        }


/*code removed*/

        //Action Listener for Female Radio button
        private void femaleRadio_CheckedChanged(object sender, EventArgs e)
        {
            //code removed
        }

        //Action Listener for English Radio button
        private void englishRadio_CheckedChanged(object sender, EventArgs e)
        {
            //code removed
        }



    }
}

我对c有点陌生。这里我想做的是以编程方式触发构造函数中单选按钮的
事件处理程序。我遵循的方法是
malereradio.PerformClick()什么都不做。如何以编程方式调用构造函数中的事件处理程序?

您可以像调用任何其他方法一样调用事件处理程序:

public Form1()
{
   InitializeComponent();
   calculator = new Calculator();

   maleRadio_CheckedChanged(maleRadio, null);
   englishRadio_CheckedChanged(englishRadio, null);
}

您只需在构造函数上调用函数
femalearadio\u CheckedChanged
。或者,您可以设置radiobutton的选定索引值以触发事件:

femaleRadio_CheckedChanged(null, null);
or
femaleRadio.SelectedIndex = 0;
只需调用以下方法:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        radioButton1.CheckedChanged += radioButton1_CheckedChanged;

        //Triggering the handlers programmatically
        radioButton1_CheckedChanged(null, null);
        radioButton2_CheckedChanged(null, null);
    }

    //this handler is manually added in the constructor
    void radioButton1_CheckedChanged(object sender, EventArgs e)
    {
        Console.WriteLine("Test event 1");
    }

    //This handler is auto-generated by designer, after adding the event in the designer
    private void radioButton2_CheckedChanged(object sender, EventArgs e)
    {
        Console.WriteLine("Test event 2");
    }
}
输出将是:

测试事件1 测试事件2 测试事件1


因为在构造函数完成后,radiobutton1将被选择为默认值。

radio\u checkChanged是否不起作用?嗯,您正在执行
PerformClick
,但希望处理
CheckedChanged
。Eren Ersönmez的答案应该有用:)我很确定这就是OP想要的+1:)啊,太好了!谢谢!但是,我可以看到单选按钮在选中时不可见,即使它们执行了事件处理程序。我怎样才能在点击它们时使其可见?@Knight您是否已经解决了最后一个问题(因为您已将此标记为答案)?我基本上直接回答了您关于“如何触发事件处理程序”的问题,但是,如果您想实际设置单选按钮的选中状态,则需要执行'maleRadio.checked=true;'相反