using System;
using System.Windows.Forms;
namespace CSharpGUIExample
{
public class SimpleForm : Form
{
private Button button;
private Label label;
public SimpleForm()
{
// 设置窗体的标题
this.Text = "C# GUI 示例";
this.Size = new System.Drawing.Size(300, 200);
// 创建一个按钮并设置其属性
button = new Button();
button.Text = "点击我";
button.Location = new System.Drawing.Point(100, 50);
button.Click += new EventHandler(Button_Click);
// 创建一个标签并设置其属性
label = new Label();
label.Text = "欢迎使用C# GUI";
label.Location = new System.Drawing.Point(100, 100);
// 将控件添加到窗体中
this.Controls.Add(button);
this.Controls.Add(label);
}
// 按钮点击事件处理程序
private void Button_Click(object sender, EventArgs e)
{
label.Text = "你点击了按钮!";
}
[STAThread]
static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new SimpleForm());
}
}
}
SimpleForm
类继承自 Form
,表示一个简单的 Windows 窗体应用程序。Button
) 和一个标签 (Label
),并设置了它们的位置和初始文本。Button_Click
,当用户点击按钮时,会更新标签的文本。Main
方法是应用程序的入口点,它启动了应用程序的消息循环,并显示了 SimpleForm
窗体。这个示例展示了如何使用 C# 和 Windows Forms 创建一个简单的图形用户界面 (GUI) 应用程序。
上一篇:internal c#
下一篇:c#复制文件夹到指定文件夹
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站