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

c#界面开发

作者:冫LOVE灬丶棒棒   发布日期:2025-04-17   浏览:115

using System;
using System.Windows.Forms;

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

        public MainForm()
        {
            // 初始化按钮控件
            button1 = new Button
            {
                Text = "点击我",
                Location = new System.Drawing.Point(50, 50),
                Width = 100,
                Height = 30
            };
            button1.Click += new EventHandler(Button1_Click);

            // 初始化标签控件
            label1 = new Label
            {
                Text = "欢迎使用C#界面开发",
                Location = new System.Drawing.Point(50, 100),
                Width = 200,
                Height = 30
            };

            // 将控件添加到窗体中
            this.Controls.Add(button1);
            this.Controls.Add(label1);

            // 设置窗体属性
            this.Text = "C#界面开发示例";
            this.Width = 300;
            this.Height = 200;
        }

        // 按钮点击事件处理方法
        private void Button1_Click(object sender, EventArgs e)
        {
            label1.Text = "你点击了按钮!";
        }

        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new MainForm());
        }
    }
}

解释说明:

  1. 命名空间和类

    • CSharpUIExample 是一个命名空间,用于组织代码。
    • MainForm 类继承自 Form 类,表示主窗体。
  2. 控件初始化

    • 创建了一个 Button 控件 (button1) 并设置了它的位置、大小和文本内容。同时为按钮的 Click 事件注册了一个事件处理方法 Button1_Click
    • 创建了一个 Label 控件 (label1) 并设置了它的位置、大小和初始文本内容。
  3. 事件处理

    • Button1_Click 方法是按钮点击事件的处理逻辑,当用户点击按钮时,会更新标签的文本内容。
  4. 窗体设置

    • 设置了窗体的标题、宽度和高度,并将控件添加到窗体中。
  5. 程序入口

    • Main 方法是应用程序的入口点,创建并运行 MainForm 实例。

通过这段代码,你可以创建一个简单的 Windows 窗体应用程序,其中包含一个按钮和一个标签。点击按钮后,标签的文本会发生变化。

上一篇:c# default

下一篇:c# 读取文件内容

大家都在看

c# 二进制

c# tcp client

c# type.gettype

c# sqlconnection

.net和c#

c#游戏开发

c#网络编程

c# rectangle

c# if else

c# rtsp

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

Laravel 中文站