Go homepage(回首页)
Upload pictures (上传图片)
Write articles (发文字帖)

The author:(作者)qq
published in(发表于) 2014/7/11 9:15:57
C#中CheckBox控件应用实例

C#中CheckBox控件应用实例

CheckBox控件

1.功能

CheckBox控件指定某个特定条件是处于打开状态还是处于关闭状态,它常用于为用户提供“是/否”或“真/假”选项。图1为CheckBox控件。



图1 CheckBox控件

2.属性

CheckBox控件常用属性及说明如表1所示。



表1 CheckBox控件常用属性及说明

下面详细介绍Checked属性,该属性用于指定复选框是否处于选中状态。

语法:

public bool Checked { get; set; }

属性值:如果CheckBox处于选定状态,则为True;否则为False。默认值为False。

注意:如果将ThreeState属性设置为True,则Checked属性将为Checked或Indeterminate CheckState返回True。

示例

使用Checked属性判断复选框是否选中

本示例使用Checked属性判断复选框是否选中,并给出提示信息。示例运行结果如图2所示。



图2 使用Checked属性判断复选框是否选中

程序主要代码如下:

if (this.checkBox1.Checked == False && this.checkBox2.Checked ==False && checkBox3.Checked == False)

{

MessageBox.Show("至少选一项爱好");

return;

}

if (radioButton1.Checked == False && radioButton2.Checked == False)

{

MessageBox.Show("请选择性别");

return;

}

完整程序代码如下:

★★★★★主程序文件完整程序代码★★★★★

using System;

using System.Collections.Generic;

using System.Windows.Forms;

namespace _8_10

{

static class Program

{

///



/// 应用程序的主入口点。

///


[STAThread]

static void Main()

{

Application.EnableVisualStyles();

Application.SetCompatibleTextRenderingDefault(false);

Application.Run(new frmCheckBox());

}

}

}

★★★★★frmCheckBox窗体设计文件完整程序代码★★★★★

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

namespace _8_10

{

public partial class frmCheckBox : Form

{

public frmCheckBox()

{

InitializeComponent();

}

private void button1_Click(object sender, EventArgs e)

{

if (this.checkBox1.Checked == false && this.checkBox2.Checked ==false && checkBox3.Checked == false)

{

MessageBox.Show("至少选一项爱好");

return;

}

if (radioButton1.Checked == false && radioButton2.Checked == false)

{

MessageBox.Show("请选择性别");

return;

}

}

private void button2_Click(object sender, EventArgs e)

{

Application.Exit();

}

private void checkBox1_CheckedChanged(object sender, EventArgs e)

{

}

private void checkBox2_CheckedChanged(object sender, EventArgs e)

{

}

private void checkBox3_CheckedChanged(object sender, EventArgs e)

{

}

private void button3_Click(object sender, EventArgs e)

{

}

private void button4_Click(object sender, EventArgs e)

{

}

private void checkedListBox1_Click(object sender, EventArgs e)

{

}

private void frmCheckBox_Load(object sender, EventArgs e)

{

}

}

}

★★★★★frmCheckBox窗体代码文件完整程序代码★★★★★

namespace _8_10

{

partial class frmCheckBox

{

///

/// 必需的设计器变量。

///


private System.ComponentModel.IContainer components = null;

///

/// 清理所有正在使用的资源。

///


/// 如果应释放托管资源,为 true;否则为 false。

protected override void Dispose(bool disposing)

{

if (disposing && (components != null))

{

components.Dispose();

}

base.Dispose(disposing);


If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)





QQ:154298438
QQ:417480759