using System;
using System.Windows.Forms;
namespace CheckBoxExample
{
public class CheckBoxDemo : Form
{
private CheckBox checkBox1;
public CheckBoxDemo()
{
// 初始化CheckBox控件
checkBox1 = new CheckBox();
checkBox1.Text = "Check me!";
checkBox1.Location = new System.Drawing.Point(50, 50);
checkBox1.CheckedChanged += new EventHandler(CheckBox_CheckedChanged);
// 将CheckBox添加到窗体
this.Controls.Add(checkBox1);
// 设置窗体属性
this.Text = "C# CheckBox Example";
this.Size = new System.Drawing.Size(300, 200);
}
// CheckBox状态改变时触发的事件处理程序
private void CheckBox_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked)
{
MessageBox.Show("Checkbox is checked!");
}
else
{
MessageBox.Show("Checkbox is unchecked!");
}
}
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.Run(new CheckBoxDemo());
}
}
}
System
和System.Windows.Forms
命名空间,定义了一个名为CheckBoxDemo
的类,继承自Form
。CheckBoxDemo
的构造函数中,初始化并配置了一个CheckBox
控件,并将其添加到窗体上。CheckBox
的状态(选中或未选中)发生变化时,会触发CheckBox_CheckedChanged
事件处理程序。根据CheckBox
的状态显示相应的消息框。Main
方法是应用程序的入口点,启动窗体应用程序。这段代码展示了如何在C# WinForms应用程序中使用CheckBox
控件,并处理其状态变化事件。
上一篇:c# 字典排序
下一篇:c#格式化字符串
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站