面向对象的理解
namespace OOP
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
/*表示按钮button对象作为一个object参数发送给事件处理程序,
该事件处理程序把参数强制转化为button类型,然后修改对象
的Text属性,改变显示的文本。*/
((Button)sender).Text = "Clicked!";
Button newButton = new Button();
newButton.Text = "New Button!";
/*通过一些重载运算符把这个事件处理程序注册为Click事件的
监听程序。同时使用非默认的构造函数创建一个新的EventHander对象
其对象是新事件处理函数的名称:*/
newButton.Click += new EventHandler(newButton_Click);
/*利用Controls属性,这个属性是一个对象,是窗体上所有控件
的集合,通过它的Add()方法把新按钮添加到窗体上。*/
Controls.Add(newButton );
}
/// <summary>
/// 添加一个新的事件处理程序,以响应新按钮生成的Click事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void newButton_Click(object sender, System.EventArgs e)
{
((Button)sender).Text = "Clicked!";
}
}
}
浙公网安备 33010602011771号