var panel1 = new Panel()
{
Size = new Size(500, 200),
Location = new Point(10, i),
BorderStyle = BorderStyle.FixedSingle
};
panel1.Controls.Add(new Button() { Text = "hi", Location = new Point(10, 20) });
这里我们可以在面板中添加一个按钮......但是如何使用该按钮..我的意思是如何在点击时处理响应?
答案 0 :(得分:0)
实例化按钮,将其分配给变量,设置事件处理程序,然后将其添加到面板:
var button = new Button { Text = "hi", Location = new Point(10, 20) };
button.Click += MyClickHandler;
panel1.Controls.Add(button);
MyClickHandler
是您的方法。
答案 1 :(得分:0)
更好的方法是:
Button sample = new Button();
sample.Name = "btn1";
sample.Text = "hi";
sample.Location = new Point(10, 20);
sample.Click += button1_Click;
panel1.Controls.Add(sample);
并定义您的方法:
private void button1_Click(object sender, EventArgs e)
{ }