using System;
using System.Windows.Forms;
namespace WinFormExample
{
public class MainForm : Form
{
private Button button1;
private Label label1;
public MainForm()
{
// 初始化按钮
button1 = new Button
{
Text = "点击我",
Location = new System.Drawing.Point(50, 50),
Width = 100,
Height = 50
};
button1.Click += new EventHandler(Button1_Click);
// 初始化标签
label1 = new Label
{
Text = "未点击",
Location = new System.Drawing.Point(50, 120),
Width = 100,
Height = 50
};
// 将控件添加到窗体
this.Controls.Add(button1);
this.Controls.Add(label1);
// 设置窗体属性
this.Text = "C# WinForm 示例";
this.Width = 300;
this.Height = 250;
}
// 按钮点击事件处理程序
private void Button1_Click(object sender, EventArgs e)
{
label1.Text = "已点击!";
}
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
}
}
}
命名空间和类定义:代码中使用了 System
和 System.Windows.Forms
命名空间。MainForm
类继承自 Form
,表示一个 Windows 窗体应用程序的主窗口。
控件初始化:
Button
控件 (button1
) 和一个 Label
控件 (label1
)。Button1_Click
。事件处理:
Button1_Click
方法会被调用,将标签的文本从“未点击”改为“已点击!”。窗体设置:
入口点:
Main
方法是程序的入口点,调用了 Application.Run(new MainForm())
来启动应用程序并显示主窗体。上一篇:c# 单例
下一篇:c# begininvoke
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站