2013-09-28 83 views
-3

動態添加按鈕座標X = 100,Y = 200;並通過點擊,表單必須將背景顏色更改爲紅色,通過第二次點擊綠色。如何將動態按鈕及其事件添加到Windows窗體?

public partial class Form1 : Form 

{ 
    public Form1() 
    { 
     InitializeComponent(); 
    } 
    private int X = 100; 
    private int Y = 200; 
    Button btn = new Button(); 
    private void Form1_Load(object sender, EventArgs e) 
    { 
     this.Controls.Add(btn); 
     btn.Location = new Point(X, Y); 
     btn.Text = "Click"; 
     btn.Click += new EventHandler(Mouse_Click); 
    } 


    private void Mouse_Click(Object s, EventArgs e) 
    { 
     // what to do here to make button change Form color? 
    } 
} 
+0

歡迎#1。這對這個網站來說確實是一個糟糕的問題。請閱讀[常見問題]和[問]結果.. –

+0

@Geo Man看起來你已經有你的解決方案,爲什麼你問/ – 2013-09-28 12:00:36

+0

private void Mouse_Click(Object s,EventArgs e) { //做什麼這裏讓按鈕更改窗體顏色? } :\ – AYETY

回答

0
//Create new instance of button 
Button btn = new Button(); 
//Set button Location 
btn.Location = new Point(100, 200); 
//Set button event handler 
btn.Click += new EventHandler(btn_Click); 
//Add button to Form 
this.Controls.Add(btn); 

void btn_click (object sender, EventArgs e) 
{ 

} 

要改變形式彩色

void btn_click (object sender, EventArgs e) 
{ 
    this.BackColor = Color.Blue; 
    //or 
    this.ForeColor = Color.Blue; 
} 
+0

@ GeoMan很高興我能幫到你 – 2013-09-28 12:34:52

相關問題