using System;
using System.Windows.Forms;
namespace WinFormExample
{
public class MainForm : Form
{
private Button button1;
private Label label1;
public MainForm()
{
// 初始化按钮
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 = "WinForm C# 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 MainForm());
}
}
}
Form,是应用程序的主窗口。MainForm 的构造函数中初始化按钮和标签,并设置它们的位置、大小和初始文本。还将这些控件添加到窗体的控件集合中。MainForm 窗体。希望这段代码和解释对你有帮助!
上一篇:c# internal
下一篇:c# split
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站