-3

动态添加坐标 X=100, Y=200 的按钮;通过单击,表单必须将背景颜色更改为红色,第二次单击变为绿色。

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?
    }
}
4

1 回答 1

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;
}
于 2013-09-28T11:58:35.087 回答