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

c# gui

作者:ー辈ふ只寵你人   发布日期:2025-02-07   浏览:74

using System;
using System.Windows.Forms;

namespace CSharpGUIExample
{
    public class SimpleForm : Form
    {
        private Button button;
        private Label label;

        public SimpleForm()
        {
            // 设置窗体的标题
            this.Text = "C# GUI 示例";
            this.Size = new System.Drawing.Size(300, 200);

            // 创建一个按钮并设置其属性
            button = new Button();
            button.Text = "点击我";
            button.Location = new System.Drawing.Point(100, 50);
            button.Click += new EventHandler(Button_Click);

            // 创建一个标签并设置其属性
            label = new Label();
            label.Text = "欢迎使用C# GUI";
            label.Location = new System.Drawing.Point(100, 100);

            // 将控件添加到窗体中
            this.Controls.Add(button);
            this.Controls.Add(label);
        }

        // 按钮点击事件处理程序
        private void Button_Click(object sender, EventArgs e)
        {
            label.Text = "你点击了按钮!";
        }

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

解释说明:

  • 命名空间和类SimpleForm 类继承自 Form,表示一个简单的 Windows 窗体应用程序。
  • 控件创建:在构造函数中,创建了一个按钮 (Button) 和一个标签 (Label),并设置了它们的位置和初始文本。
  • 事件处理:为按钮的点击事件注册了一个事件处理程序 Button_Click,当用户点击按钮时,会更新标签的文本。
  • 主程序入口Main 方法是应用程序的入口点,它启动了应用程序的消息循环,并显示了 SimpleForm 窗体。

这个示例展示了如何使用 C# 和 Windows Forms 创建一个简单的图形用户界面 (GUI) 应用程序。

上一篇:internal c#

下一篇: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 中文站