using System;
using System.Windows.Forms;
namespace CSharpUIExample
{
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 = 30
};
button1.Click += new EventHandler(Button1_Click);
// 初始化标签控件
label1 = new Label
{
Text = "欢迎使用C#界面开发",
Location = new System.Drawing.Point(50, 100),
Width = 200,
Height = 30
};
// 将控件添加到窗体中
this.Controls.Add(button1);
this.Controls.Add(label1);
// 设置窗体属性
this.Text = "C#界面开发示例";
this.Width = 300;
this.Height = 200;
}
// 按钮点击事件处理方法
private void Button1_Click(object sender, EventArgs e)
{
label1.Text = "你点击了按钮!";
}
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
}
}
}
命名空间和类:
CSharpUIExample
是一个命名空间,用于组织代码。MainForm
类继承自 Form
类,表示主窗体。控件初始化:
Button
控件 (button1
) 并设置了它的位置、大小和文本内容。同时为按钮的 Click
事件注册了一个事件处理方法 Button1_Click
。Label
控件 (label1
) 并设置了它的位置、大小和初始文本内容。事件处理:
Button1_Click
方法是按钮点击事件的处理逻辑,当用户点击按钮时,会更新标签的文本内容。窗体设置:
程序入口:
Main
方法是应用程序的入口点,创建并运行 MainForm
实例。通过这段代码,你可以创建一个简单的 Windows 窗体应用程序,其中包含一个按钮和一个标签。点击按钮后,标签的文本会发生变化。
上一篇:c# default
下一篇:c# 读取文件内容
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站