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