CheckedListBox 使用 - asp.net 教程-
时间:2010-08-26 来源:星空3
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace CheckedListBoxExample
{
public partial class FormCheckedListBox : Form
{
public FormCheckedListBox()
{
InitializeComponent();
}
private void buttonOK_Click(object sender, EventArgs e)
{
string mystr = "您所选择的课程是:";
// checkedListBox1.CheckedItems.Count是用户选择项目的个数
int checkedNumber = checkedListBox1.CheckedItems.Count;
if (checkedNumber == 0)
{
MessageBox.Show("您还没有选择任何课程!");
}
else
{
for (int i = 0; i < checkedNumber; i++)
{
// checkedListBox1.CheckedItems[i]是用户选中项集合中的第i项
mystr += checkedListBox1.CheckedItems[i] + " ; ";
}
//如果尾部有分号,则将尾部的分号变为叹号
if (mystr[mystr.Length - 2] == ';')
{
mystr = mystr.Substring(0, mystr.Length - 2);
mystr += "!";
}
MessageBox.Show(mystr);
}
}
private void buttonReference_Click(object sender, EventArgs e)
{
//取消已经选中的项
for (int i = 0; i < checkedListBox1.Items.Count; i++)
{
checkedListBox1.SetItemChecked(i, false);
checkedListBox1.SetSelected(i, false);
}
//自动添加建议选中的项
checkedListBox1.SetItemChecked(0, true);
checkedListBox1.SetItemChecked(3, true);
checkedListBox1.SetItemChecked(7, true);
}
}
}