2011-11-21 36 views
2
Button bn = new Button(); 
bn.Location = new System.Drawing.Point(560, 350); 
bn.Name = "btnDelete"; 
bn.Text = "Delete"; 
bn.Size = new System.Drawing.Size(100, 50); 
myTabPage.Controls.Add(bn); 

我已經定位了按鈕,我將使用什麼屬性來添加代碼後面的按鈕?如何動態添加後面的代碼按鈕

+0

另外值得一讀http://msdn.microsoft.com/en-us/library/aa645739%28v=vs.71%29.aspx –

回答

4

很簡單:

bn.Click += MyClick; 

... 

private void MyClick(object sender, EventArgs e) { 
    MessageBox.Show("hello"); 
} 

在這裏,您要註冊一個點擊事件並指定運行事件觸發時的代碼。

+0

正是我正在尋找!謝謝。 – Brandon

0

你需要做一些東西來準備窗體上的按鈕

private void AddButtons() 
{ 
    // Suspend the form layout and add two buttons. 
    this.SuspendLayout(); 
    Button buttonOK = new Button(); 
    buttonOK.Location = new Point(10, 10); 
    buttonOK.Size = new Size(75, 25); 
    buttonOK.Text = "OK"; 

    this.Controls.Add(buttonOK); 
    this.ResumeLayout(); 
} 

實在是沒有「代碼背後」(由this在本例中提及,從http://msdn.microsoft.com/en-us/library/y53zat12.aspx拍攝。) - 該按鈕對象,如你所願使用它。想必你想訂閱點擊事件:

bn.Click += new System.EventHandler(this.bnClickListener); 

private void bnClickListener(object sender, EventArgs e) 
{ 
    // Stuff to do when clicked. 
}