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());
}
}
}
System
和 System.Windows.Forms
命名空间,创建了一个名为 Form1
的窗体类。Form1()
中,初始化了一个按钮 button1
,设置了按钮的文本、位置和大小。Click
) 添加了一个事件处理程序 button1_Click
,当按钮被点击时会弹出一个消息框显示 "Button was clicked!"。Main
方法是应用程序的入口点,它启动了 Windows 窗体应用程序并显示窗体。下一篇:c# 求余数
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站