All articles(网络文学目录) All Pictures(图片目录) All Softwares(软件目录)

 
C#中CheckedListBox控件应用实例

Writer: qq Article type: Programming skills(编程技巧) Time: 2014/7/11 9:16:00 Browse times: 392 Comment times: 0

C#中CheckedListBox控件应用实例


Head photo

Go homepage
Upload pictures
Write articles

C#中CheckedListBox控件应用实例

CheckedListBox控件
1. 功能
CheckedListBox控件扩展了ListBox控件,它几乎能完成列表框可以完成的所有任务,并且还可以在列表中的项旁边显示复选标记。CheckedListBox控件与ListBox控件的主要差异在于复选列表框只能有一项选中或未选中任何项。注意,选定的项在窗体上突出显示,与已选中的项不同。图1为CheckedListBox控件。

图1 CheckedListBox控件
2.属性
CheckedListBox 控件常用属性及说明如表1所示。

表1 CheckedListBox控件常用属性及说明
下面对比较重要的属性进行详细介绍。
(1)Items属性。此属性用于向控件中添加项。
语法:
public ObjectCollection Items { get; }
属性值:代表CheckedListBox中项的CheckedListBox.ObjectCollection集合。
(2)CheckedItems属性。此属性是指CheckedListBox控件中所有选中项的集合。
语法:
public CheckedItemCollection CheckedItems { get; }
属性值:CheckedListBox的CheckedListBox.CheckedItemCollection集合。
示例
Items属性的使用
本示例实现效果为:当程序运行时,单击【确定】按钮,利用Items属性将选择的项添加到Checked ListBox 控件中。
程序主要代码如下:
if (this.checkBox1.Checked == True)
{
this.checkedListBox1.Items.Add(checkBox1.Text);
}
if (this.checkBox2.Checked == True)
{
this.checkedListBox1.Items.Add(checkBox2.Text);
}
if (this.checkBox3.Checked == True)
{
this.checkedListBox1.Items.Add(checkBox3.Text);
}
3.方法
CheckedListBox控件常用方法及说明如表2所示。

表2 CheckedListBox控件常用方法及说明
下面详细介绍SetItemCheckState方法,此方法用于设置当前的复选状态。
语法:
public void SetItemCheckState (int index,CheckState value)
index:要为其设置状态的项的索引。
value:CheckState值之一。
完整程序代码如下:
★★★★★主程序文件完整程序代码★★★★★
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;
}
this.checkedListBox1.Items.Clear();//清除当前数据
if (this.checkBox1.Checked == true)
{
this.checkedListBox1.Items.Add(checkBox1.Text);
}
if (this.checkBox2.Checked == true)
{
this.checkedListBox1.Items.Add(checkBox2.Text);
}
if (this.checkBox3.Checked == true)
{
this.checkedListBox1.Items.Add(checkBox3.Text);
}
if (this.radioButton1.Checked == true)
{
this.checkedListBox1.Items.Add(radioButton1.Text);
}
if (this.radioButton2.Checked == true)
{
this.checkedListBox1.Items.Add(radioButton2.Text);
}
this.checkBox1.Checked = false;
this.checkBox2.Checked = false;
this.checkBox3.Checked = false;




There are 0 records,
Comment:
Must be registered users to comment(必须是注册用户才能发表评论)

Disclaimer Privacy Policy About us Site Map
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.