using System;
using System.Windows.Forms;
namespace CSharpControlsExample
{
public class MainForm : Form
{
private Button button1;
private Label label1;
private TextBox textBox1;
public MainForm()
{
// 初始化控件
button1 = new Button();
label1 = new Label();
textBox1 = new TextBox();
// 设置按钮属性
button1.Text = "点击我";
button1.Location = new System.Drawing.Point(100, 50);
button1.Click += new EventHandler(Button1_Click);
// 设置标签属性
label1.Text = "请输入内容:";
label1.Location = new System.Drawing.Point(100, 100);
// 设置文本框属性
textBox1.Location = new System.Drawing.Point(200, 100);
textBox1.Size = new System.Drawing.Size(150, 20);
// 将控件添加到窗体
this.Controls.Add(button1);
this.Controls.Add(label1);
this.Controls.Add(textBox1);
// 设置窗体属性
this.Text = "C# 控件示例";
this.Size = new System.Drawing.Size(400, 300);
}
// 按钮点击事件处理程序
private void Button1_Click(object sender, EventArgs e)
{
MessageBox.Show("你输入的内容是: " + textBox1.Text);
}
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
}
}
}
这段代码展示了一个简单的 Windows Forms 应用程序,包含三个主要的控件:Button、Label 和 TextBox。
初始化控件:
button1 是一个按钮控件,用户可以点击它触发事件。label1 是一个标签控件,用于显示静态文本。textBox1 是一个文本框控件,允许用户输入文本。设置控件属性:
Location) 和大小 (Size)。Text) 和点击事件 (Click)。Text)。将控件添加到窗体:
this.Controls.Add() 方法将控件添加到窗体中。设置窗体属性:
Text) 和大小 (Size)。事件处理:
Button1_Click,当用户点击按钮时,会弹出一个消息框显示文本框中的内容。应用程序入口:
Main 方法是应用程序的入口点,创建并运行 MainForm 窗体。上一篇:websocket c#
下一篇:c# char转string
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站