using System;
using System.Windows.Forms;
namespace WinFormUIExample
{
public class MainForm : Form
{
private Button button1;
private Label label1;
private TextBox textBox1;
public MainForm()
{
// 初始化组件
InitializeComponents();
}
private void InitializeComponents()
{
// 创建按钮
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)
};
// 创建文本框
textBox1 = new TextBox
{
Location = new System.Drawing.Point(50, 160),
Width = 200
};
// 将控件添加到窗体
this.Controls.Add(button1);
this.Controls.Add(label1);
this.Controls.Add(textBox1);
// 设置窗体属性
this.Text = "C# WinForms UI Example";
this.Width = 300;
this.Height = 300;
}
private void Button1_Click(object sender, EventArgs e)
{
// 按钮点击事件处理程序
label1.Text = "Button Clicked!";
textBox1.Text = "You clicked the button.";
}
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
}
}
}
WinFormUIExample 的命名空间,其中包含一个继承自 Form 的 MainForm 类。MainForm 构造函数调用 InitializeComponents 方法来初始化窗体上的控件。InitializeComponents 方法中,创建了三个控件:Button、Label 和 TextBox。每个控件设置了位置、大小和初始文本。Button1_Click,当按钮被点击时,会更新标签和文本框的内容。Main 方法是应用程序的入口点,它启动了 Windows Forms 应用程序的消息循环,并显示 MainForm 窗体。通过这段代码,你可以创建一个简单的 Windows Forms 应用程序,其中包括一个按钮、一个标签和一个文本框,并实现按钮点击后的简单交互。
上一篇:c#字符串替换
下一篇:c# ?:
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站