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

c#winform

作者:慢靈魂   发布日期:2025-03-13   浏览:430

using System;
using System.Windows.Forms;

namespace WinFormExample
{
    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 = 50
            };
            button1.Click += new EventHandler(Button1_Click);

            // 初始化标签
            label1 = new Label
            {
                Text = "未点击",
                Location = new System.Drawing.Point(50, 120),
                Width = 100,
                Height = 50
            };

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

            // 设置窗体属性
            this.Text = "C# WinForm 示例";
            this.Width = 300;
            this.Height = 250;
        }

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

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

解释说明:

  1. 命名空间和类定义:代码中使用了 SystemSystem.Windows.Forms 命名空间。MainForm 类继承自 Form,表示一个 Windows 窗体应用程序的主窗口。

  2. 控件初始化

    • 创建了一个 Button 控件 (button1) 和一个 Label 控件 (label1)。
    • 设置了按钮的文本、位置、宽度和高度,并为按钮的点击事件绑定了一个事件处理程序 Button1_Click
    • 标签控件用于显示点击状态。
  3. 事件处理

    • 当用户点击按钮时,Button1_Click 方法会被调用,将标签的文本从“未点击”改为“已点击!”。
  4. 窗体设置

    • 设置了窗体的标题、宽度和高度,并将按钮和标签添加到窗体的控件集合中。
  5. 入口点

    • Main 方法是程序的入口点,调用了 Application.Run(new MainForm()) 来启动应用程序并显示主窗体。

上一篇:c# 单例

下一篇:c# begininvoke

大家都在看

c# 二进制

c# tcp client

c# type.gettype

c# sqlconnection

.net和c#

c#游戏开发

c#网络编程

c# rectangle

c# if else

c#高并发

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

Laravel 中文站