2017-06-20 51 views
0

我已經編寫了一個程序,該程序相當於數千行代碼和許多按鈕。我現在希望從另一個Button模擬此代碼。我在下面寫了一個較小的程序來模擬我想要做的事情。我已經看過其他的例子,看起來好像它會很簡單,但如何做!如何模擬鼠標/按鈕在c#中單擊#

Button1.PerformClick(); 

不會編譯,但如何模擬按鈕單擊?

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

namespace WindowsFormsApp13 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void button3_Click(object sender, EventArgs e) 
     { 
      //Some simulation code clcick button 2/3 
      //Button1.PerformClick(); 
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      MessageBox.Show("Button1.Clicked"); 
     } 

     private void button2_Click(object sender, EventArgs e) 
     { 
      MessageBox.Show("Button2.Clicked"); 
     } 
    } 
} 
+0

您是否想模擬鼠標單擊事件或運行事件單擊事件處理程序只? – Alexander

+0

是否要模擬鼠標點擊?是的,我認爲?我想重複使用代碼而不是從1千行到5行,這是在運行時pls – Data

+1

你的button1來自哪裏?你有錯字嗎?顯示爲您的designer.cs的代碼 –

回答

1

我想你犯了一個拼寫錯誤。它應該是button3而不是Button3。相同的代碼爲我工作:

public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     MessageBox.Show("Button1.Clicked"); 
    } 

    private void button2_Click(object sender, EventArgs e) 
    { 
     MessageBox.Show("Button2.Clicked"); 
    } 

    private void button3_Click(object sender, EventArgs e) 
    { 
     button1.PerformClick(); 
    }  
}