Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

c# winform ui

作者:岁月凉茶   发布日期:2026-03-03   浏览:80

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());
        }
    }
}

解释说明:

  1. 命名空间和类:代码定义了一个名为 WinFormUIExample 的命名空间,其中包含一个继承自 FormMainForm 类。
  2. 构造函数MainForm 构造函数调用 InitializeComponents 方法来初始化窗体上的控件。
  3. 控件创建:在 InitializeComponents 方法中,创建了三个控件:ButtonLabelTextBox。每个控件设置了位置、大小和初始文本。
  4. 事件处理:为按钮的点击事件绑定了一个事件处理程序 Button1_Click,当按钮被点击时,会更新标签和文本框的内容。
  5. 主程序入口Main 方法是应用程序的入口点,它启动了 Windows Forms 应用程序的消息循环,并显示 MainForm 窗体。

通过这段代码,你可以创建一个简单的 Windows Forms 应用程序,其中包括一个按钮、一个标签和一个文本框,并实现按钮点击后的简单交互。

上一篇:c#字符串替换

下一篇:c# ?:

大家都在看

c# 二进制

c# 创建目录

c# datatable group by

c# tcp client

c# type.gettype

c# sqlconnection

c# string.format 小数位数

.net和c#

c#获取系统时间

c#游戏开发

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站