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

c# button

作者:△冷色彩   发布日期:2025-09-11   浏览:34

using System;
using System.Windows.Forms;

namespace CSharpButtonExample
{
    public class Form1 : Form
    {
        private Button button1;

        public Form1()
        {
            // 初始化按钮控件
            button1 = new Button();
            button1.Text = "Click Me";
            button1.Location = new System.Drawing.Point(50, 50);
            button1.Size = new System.Drawing.Size(100, 50);

            // 添加按钮点击事件处理程序
            button1.Click += new EventHandler(button1_Click);

            // 将按钮添加到窗体中
            this.Controls.Add(button1);

            // 设置窗体属性
            this.Text = "C# Button Example";
            this.Size = new System.Drawing.Size(280, 200);
        }

        // 按钮点击事件处理程序
        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Button was clicked!");
        }

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

解释说明:

  1. 命名空间和类:代码使用了 SystemSystem.Windows.Forms 命名空间,创建了一个名为 Form1 的窗体类。
  2. 按钮初始化:在构造函数 Form1() 中,初始化了一个按钮 button1,设置了按钮的文本、位置和大小。
  3. 事件处理:为按钮的点击事件 (Click) 添加了一个事件处理程序 button1_Click,当按钮被点击时会弹出一个消息框显示 "Button was clicked!"。
  4. 窗体设置:设置了窗体的标题和大小,并将按钮添加到窗体的控件集合中。
  5. 主方法Main 方法是应用程序的入口点,它启动了 Windows 窗体应用程序并显示窗体。

上一篇:c#修改datatable的某个数值

下一篇:c# 求余数

大家都在看

c# 二进制

c# tcp client

c# type.gettype

c# sqlconnection

c# string.format 小数位数

.net和c#

c#获取系统时间

c#游戏开发

c#网络编程

c# rectangle

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

Laravel 中文站