Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/288.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# 根据IF语句自动调用按钮点击事件_C#_Visual Studio 2010_Visual Studio 2012 - Fatal编程技术网

C# 根据IF语句自动调用按钮点击事件

C# 根据IF语句自动调用按钮点击事件,c#,visual-studio-2010,visual-studio-2012,C#,Visual Studio 2010,Visual Studio 2012,如果条件为真,我尝试调用Button_click事件。但由于某些原因,它不起作用。你能帮忙吗? 多谢各位 namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); button1.Click+=button1_Click; } private void Che

如果条件为真,我尝试调用Button_click事件。但由于某些原因,它不起作用。你能帮忙吗? 多谢各位

namespace WindowsFormsApplication1
{

public partial class Form1 : Form
{

    public Form1()
    {
        InitializeComponent();
       button1.Click+=button1_Click;

    }


    private void CheckFile()
    {
        FileInfo info = new FileInfo("c:\\test.txt");
        if (info.Length > 0)
        {
            button1.PerformClick();
          //this.button1.Click += new EventHandler(button1_Click);
            MessageBox.Show("FILE is not empty"); //just for check

        }
    }                   

    private void button1_Click(object sender, EventArgs e)
    {            
            MessageBox.Show("Message POP UP");            
    }

public static void main(String[] args)
{
    Form1 f = new Form1();
    f.CheckFile();
    MessageBox.Show("CheckFile called");    

}
}
}像这样调用它

button1_Click(this.button1, new EventArgs())

您需要向按钮单击事件处理程序发送两个参数

1.事件的根源。 2.事件参数

试试这个:

        object sender = new object() ;
        EventArgs e=new EventArgs();
        button1_Click(sender,e);
简单地 只需修改按钮单击方法,如下所示:

private void button1_Click(EventArgs e)
{            
        MessageBox.Show("Message POP UP");            
}
这样称呼它:

button1_Click(new EventArgs);
试试这个:

//but is the Button you wanna click
buttonClick(but, new MouseEventArgs(MouseButtons.Left , 1, 0, 0, 0 ));