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

c# ui

作者:忽然之间   发布日期:2026-04-29   浏览:118

using System;
using System.Windows.Forms;

namespace CSharpUIExample
{
    public class SimpleForm : Form
    {
        private Button button1;
        private Label label1;

        public SimpleForm()
        {
            // 初始化按钮
            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 = "C# UI 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 SimpleForm());
        }
    }
}

解释说明:

  1. 命名空间和类:代码使用 SystemSystem.Windows.Forms 命名空间来创建一个简单的 Windows 窗体应用程序。SimpleForm 类继承自 Form,表示一个窗口。

  2. 控件初始化

    • 创建了一个按钮 (button1) 和一个标签 (label1)。
    • 设置了按钮的文本、位置、宽度和高度,并为按钮的点击事件绑定了一个事件处理程序 Button1_Click
    • 设置了标签的文本、位置、宽度和高度。
  3. 事件处理:当用户点击按钮时,Button1_Click 方法会被调用,将标签的文本更改为 "Button Clicked!"。

  4. 窗体属性:设置了窗体的标题、宽度和高度。

  5. 主程序入口Main 方法是程序的入口点,它启动了 Windows 窗体应用程序,并显示 SimpleForm 窗体。

上一篇:c# picturebox

下一篇:c#单例模式

大家都在看

c# 二进制

c# 创建目录

c# socket服务端连接多个客户端

c# datatable group by

c# tcp client

c# type.gettype

c# sqlconnection

c# string.format 小数位数

c# invoke方法

.net和c#

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

Laravel 中文站