这在C#中被认为是一种不好的做法吗?
我的英语不是很好,我主要用java开发,我试图为clickevent创建一个外部监听器,但我一直觉得这是错误的 以下是我的例子: 表格这在C#中被认为是一种不好的做法吗?,c#,.net,events,C#,.net,Events,我的英语不是很好,我主要用java开发,我试图为clickevent创建一个外部监听器,但我一直觉得这是错误的 以下是我的例子: 表格 public partial class MainFrame : System.Windows.Forms.Form { private System.ComponentModel.Container components = null; private System.Windows.Forms.Button startButton;
public partial class MainFrame : System.Windows.Forms.Form
{
private System.ComponentModel.Container components = null;
private System.Windows.Forms.Button startButton;
public MainFrame()
{
initGUI();
}
private void initGUI()
{
this.components = new System.ComponentModel.Container();
this.Text = "CustomGUI";
ShowIcon = false;
this.startButton = new System.Windows.Forms.Button();
startButton.Text = "Start";
startButton.SetBounds(0, 0, 100, 25);
this.Controls.Add(this.startButton);
MainEventListener listen = new MainEventListener();
startButton.Click += listen.startClicked;
}
}
下一个:听众
class MainEventListener
{
public void startClicked(object sender, System.EventArgs e)
{
Application.Exit();
}
}
最后:主要方法
class ApplicationStarter
{
static void Main(string[] args)
{
MainFrame frame = new MainFrame();
System.Windows.Forms.Application.Run(frame);
}
}
我总觉得自己做得不对
这不会添加像netbeans这样的无用代码吗?我不知道netbeans,但请尝试一下。没有太多的脂肪。它添加了大量额外的代码,但隐藏在设计器文件中。它隐藏了组件定位和事件关联。您只需要在VisualStudio的事件设置中说“onClick”do“xyz”。然后,它将在代码中为您创建一个事件处理程序,如果您想要重命名,那么您可以重命名该代码,假设这是您的主窗体(此处使用类名),默认情况下,当窗体关闭时,您的应用程序将退出。