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

c# checkbox

作者:嗜神魔影   发布日期:2025-10-03   浏览:99

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());
        }
    }
}

解释说明:

  • 命名空间和类:代码使用了SystemSystem.Windows.Forms命名空间,定义了一个名为CheckBoxDemo的类,继承自Form
  • 构造函数:在CheckBoxDemo的构造函数中,初始化并配置了一个CheckBox控件,并将其添加到窗体上。
  • 事件处理:当CheckBox的状态(选中或未选中)发生变化时,会触发CheckBox_CheckedChanged事件处理程序。根据CheckBox的状态显示相应的消息框。
  • 主方法Main方法是应用程序的入口点,启动窗体应用程序。

这段代码展示了如何在C# WinForms应用程序中使用CheckBox控件,并处理其状态变化事件。

上一篇:c# 字典排序

下一篇: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 中文站