2013-12-17 3 views
0

조건이 TRUE 일 경우 Button_click 이벤트를 호출하려고합니다. 하지만 몇 가지 이유로 인해 작동하지 않습니다. 제발 도와 줄 수있어? 감사합니다.IF 문에 따라 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");  

} 
} 

}

답변

0

당신은 버튼 클릭 이벤트 핸들러에 두 개의 인수를 보낼 필요가이

button1_Click(this.button1, new EventArgs()) 
0

처럼 호출합니다.

1. 이벤트의 무음.
2. 이벤트 인수.

간단히
 object sender = new object() ; 
     EventArgs e=new EventArgs(); 
     button1_Click(sender,e); 
0

그냥 같이 귀하를 Button1_Click 방법을 수정 :

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

을 그리고 같이 호출 :

이 시도

button1_Click(new EventArgs); 
0

이 시도 :

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