public partial class CheckListBoxTest : Form { public CheckListBoxTest() { InitializeComponent(); } string connstr = "server=localhost;user id=root;password=****;persistsecurityinfo=True;database=student"; private void Form1_Load(object sender, EventArgs e) { using (MySqlConnection conn = new MySqlConnection(connstr)) { MySqlDataAdapter sda = new MySqlDataAdapter("SELECT * FROM dc_commoncourseclass", conn); DataTable dt = new DataTable(); sda.Fill(dt); checkedListBox1.DataSource = dt; checkedListBox1.ValueMember = "ClassId"; checkedListBox1.DisplayMember = "ClassName"; for(int i = 0; i < checkedListBox1.Items.Count;i++) { DataRowView dr = checkedListBox1.Items[i] as DataRowView; WriteLine(dr["ClassName"] + "," + dr["ClassId"]); if ((int)dr["ClassId"] == 3) { checkedListBox1.SetItemChecked(i, true); checkedListBox1.SetSelected(i, true); } } listBox1.SelectionMode = SelectionMode.MultiExtended; listBox1.DataSource = dt; listBox1.ValueMember = "ClassId"; listBox1.DisplayMember = "ClassName"; } } private void button1_Click(object sender, EventArgs e) { string strText = string.Empty; foreach(var o in checkedListBox1.CheckedItems) { //checkedListBox1.SetSelected(i, true); strText += checkedListBox1.GetItemText(o) + ","; } WriteLine("选择文本:" + strText); string strValue = string.Empty; foreach (int i in checkedListBox1.CheckedIndices) { checkedListBox1.SetSelected(i, true); strValue += checkedListBox1.SelectedValue + ","; } WriteLine("选择值:" + strValue); } private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { System.Diagnostics.Process.Start("http://www.baidu.com"); } private void button2_Click(object sender, EventArgs e) { foreach(var o in listBox1.SelectedItems) { WriteLine(o); } } }