button.PerformClick();不在Form Load中工作

时间:2013-10-10 15:29:20

标签: c# winforms button

我在form_load中调用按钮点击,如下所示:

public void Form1_Load(object s, EventArgs e)
{
    button.PerformClick();
}

但加载按钮后没有点击,我做错了什么?

3 个答案:

答案 0 :(得分:6)

你可以在另一个函数中点击你想要做的任何事情,然后从点击处理程序内部调用它,或者像这样以编程方式调用它 -

public void Form1_Load(object s, EventArgs e)
    {
        //button.PerformClick();
        PerformClickAction();
    }

void button_click(object sender,EventArgs e) 
{
    PerformClickAction();
}

void PerformClickAction()
{
    // Write what you need to do on click
}

答案 1 :(得分:2)

这对我有用:

public void Form1_Load(object s, EventArgs e){
  button.PerformClick();
}

看起来您没有将Form1_Load 注册为表单的Load事件的事件处理程序。试试这个:

public Form1(){
   InitializeComponent();
   Load += Form1_Load;//Register the event handler so that it will work for you.
}

答案 2 :(得分:0)

要在表单加载时单击按钮,您需要在加载表单后触发事件,试试这个

public Form1()
{
        InitializeComponent();
        //Event fired
        this.Load += new System.EventHandler(this.button1_Click);

}

//Event Handler 
private void button1_Click(object sender, EventArgs e)
{
    //do something
}