/// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void ListboxCheckboxForm_Load(object sender, EventArgs e) { //设置CheckedListBox中第i项的Checked状态 DataTable dt = new DataTable(); dt.Columns.Add("id", typeof(Guid)); dt.Columns.Add("name", typeof(string)); dt.Rows.Add(Guid.NewGuid(), "geovindu"); dt.Rows.Add(Guid.NewGuid(), "duf"); dt.Rows.Add(Guid.NewGuid(), "涂聚文"); dt.Rows.Add(Guid.NewGuid(), "tujwen"); //checkedListBox1.Items.Add(""); //checkedListBox1.Items.Insert(0, ""); checkedListBox1.DataSource = dt; checkedListBox1.DisplayMember = "name"; checkedListBox1.ValueMember = "id"; checkedListBox1.SetItemCheckState(1, CheckState.Checked); } /// <summary> /// 獲取選擇的項 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button1_Click(object sender, EventArgs e) { //1 string checkedText = string.Empty; for (int i = 0; i < this.checkedListBox1.CheckedItems.Count; i++) { this.checkedListBox1.SetSelected(i, true); checkedText += (String.IsNullOrEmpty(checkedText) ? "" : ",") + this.checkedListBox1.GetItemText(this.checkedListBox1.Items[i]) + "[" +this.checkedListBox1.SelectedValue.ToString()+"]"; } MessageBox.Show(checkedText); //2 for (int i = 0; i < checkedListBox1.Items.Count; i++) { //如果checkedListBox1的第i项被选中, //则显示checkedListBox1对应的值 if (checkedListBox1.GetItemChecked(i)) { // MessageBox.Show(checkedListBox1.GetItemText(checkedListBox1.Items[i]) + "[" + this.checkedListBox1.SelectedValue.ToString()+"]"); } } //3 string strCollected = string.Empty; for (int i = 0; i < checkedListBox1.Items.Count; i++) { if (checkedListBox1.GetItemChecked(i)) { if (strCollected == string.Empty) { strCollected = checkedListBox1.GetItemText(checkedListBox1.Items[i]); } else { strCollected = strCollected + "/" + checkedListBox1.GetItemText(checkedListBox1.Items[i]); } } } //MessageBox.Show(strCollected); } /// <summary> /// 設定是否全選 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void select_all_CheckedChanged(object sender, EventArgs e) { if (select_all.Checked) { for (int j = 0; j < checkedListBox1.Items.Count; j++) checkedListBox1.SetItemChecked(j, true); } else { for (int j = 0; j < checkedListBox1.Items.Count; j++) checkedListBox1.SetItemChecked(j, false); } } /// <summary> /// 獲取選擇的項 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button2_Click(object sender, EventArgs e) { string checkedText = string.Empty; for (int i = 0; i < this.checkedListBox1.Items.Count; i++) { if (this.checkedListBox1.GetItemChecked(i)) { this.checkedListBox1.SetSelected(i, true); checkedText += (String.IsNullOrEmpty(checkedText) ? "" : ",") +"["+this.checkedListBox1.SelectedValue.ToString()+"]" + this.checkedListBox1.GetItemText(checkedListBox1.Items[i]); } } MessageBox.Show(checkedText); } /// <summary> /// 設置選擇項 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button3_Click(object sender, EventArgs e) { checkedListBox1.DataSource = null; DataTable dt = new DataTable(); dt.Columns.Add("id", typeof(Guid)); dt.Columns.Add("name", typeof(string)); dt.Columns.Add("check", typeof(bool)); dt.Rows.Add(Guid.NewGuid(), "geovindu",false); dt.Rows.Add(Guid.NewGuid(), "duf",true); dt.Rows.Add(Guid.NewGuid(), "涂聚文",false); dt.Rows.Add(Guid.NewGuid(), "tujwen",true); checkedListBox1.DataSource = dt; checkedListBox1.DisplayMember = "name"; checkedListBox1.ValueMember = "id"; // for (int i = 0; i < dt.Rows.Count; i++) { checkedListBox1.SetItemChecked(i, (bool)dt.Rows[i]["check"]); } }