c#中在代码中定义了一个Button控件,如何添加该控件的单击事件。该事件的作用是使另一个也是自己在代码中定义的Label控件的Text属性设为“取消”。
c#中在代码中定义了一个Button控件,如何添加该控件的单击事件。该事件的作用是使另一个也是自己在代码中定义的Label控件的Text属性设为“取消”。谢谢了呀
程序代码:using System;
using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
var lbl = new Label {Text = "LabelText"};
Controls.Add(lbl);
var but = new Button
{
Text = "ButtonText",
Tag = lbl,
Location = new Point(lbl.Location.X, lbl.Location.Y + lbl.Height)
};
but.Click += But_Click;
Controls.Add(but);
}
void But_Click(object sender, EventArgs e)
{
((Label) ((Button) sender).Tag).Text = "取消";
}
}
}








