学习.NET开发// Visual C#中的Windows窗体 - 将控件链接到事件处理程序

use*_*142 3 .net c# asp.net visual-studio-2010 visual-studio

我是一名转向.NET开发的Java程序员,拥有大约5年的程序设计经验.我通常在文本编辑器中编写所有代码,省略IDE的一面.使用Visual C#我正在使用Visual Studio 2010 IDE.为了掌握界面,我遵循官方的Microsoft教程.

我的问题涉及为控件创建事件处理程序:

在设计一个Windows窗体应用程序时,可以将控件直接拖到窗体上(即按钮,复选框等).本教程指导用户双击按钮控件并双击它以在.cs文件中创建单击事件处理程序.这非常正常,并为具有(名称)showButton的按钮创建以下代码:

private void showButton_Click(object sender, EventArgs e)
{

}
Run Code Online (Sandbox Code Playgroud)

按钮和事件处理程序之间的链接存储在哪里?如,编译器如何知道上面的事件处理程序映射到哪个按钮?

Shy*_*mep 8

InitializeComponent()方法中查看YourForm.Designer.cs .

你会发现类似的代码

// 
// show
// 
this.show.Location = new System.Drawing.Point(10, 10);
this.show.Name = "show";
this.show.Size = new System.Drawing.Size(75, 23);
this.show.TabIndex = 0;
this.show.Text = "button1";
this.show.UseVisualStyleBackColor = true;
this.show.Click += new System.EventHandler(this.ShowClick);
Run Code Online (Sandbox Code Playgroud)