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

winform c#

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

using System;
using System.Windows.Forms;

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

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

解释说明:

  • MainForm 类:继承自 Form,是应用程序的主窗口。
  • button1 和 label1:分别是按钮和标签控件,用于显示文本和响应用户交互。
  • 构造函数:在 MainForm 的构造函数中初始化按钮和标签,并设置它们的位置、大小和初始文本。还将这些控件添加到窗体的控件集合中。
  • Button1_Click 方法:这是按钮的点击事件处理程序,当用户点击按钮时,会触发这个方法,将标签的文本更改为 "Button Clicked!"。
  • Main 方法:应用程序的入口点,创建并运行 MainForm 窗体。

希望这段代码和解释对你有帮助!

上一篇:c# internal

下一篇:c# split

大家都在看

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 中文站