winform基礎篇之checklisybox學習
阿新 • • 發佈:2019-02-01
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WinformAllControls { public partial class CheckBox : Form { public CheckBox() { InitializeComponent(); } private void CheckBox_Load(object sender, EventArgs e) { List<string> coures = new List<string>(); coures.Add("語文"); coures.Add("數學"); coures.Add("英語"); coures.Add("物理"); //為checklistbox賦值 foreach (string item in coures) { checklb.Items.Add(item); } } private void btn_getListItmes_Click(object sender, EventArgs e) { //獲取所有選中狀態的值 string str=""; for (int i =0; i < checklb.Items.Count; i++) { if (checklb.GetItemChecked(i)) { str +=checklb.GetItemText(checklb.Items[i]); } } MessageBox.Show(str); } private void checklb_ItemCheck(object sender, ItemCheckEventArgs e) { //實現checklistbox單選 //if (checklb.SelectedItems.Count > 0) //{ // //理論:除了指定的item項,其他項均設定為未選中,強調一點只能改變一個屬性值,切記不能迴圈觸發事件 // for (int i = 0; i < checklb.Items.Count; i++) // { // int index1 = e.Index; // if (i!= index1) // { // this.checklb.SetItemCheckState(i, CheckState.Unchecked); // } // } //} } private void checkBox1_CheckedChanged(object sender, EventArgs e) { if (checkBox1.Checked) { for (int i = 0; i < checklb.Items.Count; i++) { if (checklb.GetItemChecked(i)) { checklb.SetItemChecked(i, false); } else { checklb.SetItemChecked(i, true); } } } } } }