C# CHECKEDLISTBOX用法總結

來源:互聯網
上載者:User

一般認為:foreach (object obj in checkedListBox1.SelectedItems)即可遍曆選中的值。

其實這裡遍曆的只是高亮的值並不是打勾的值。遍曆打勾的值要用下面的代碼:

 

for (int i = 0; i < checkedListBox1.Items.Count; i++){    if (checkedListBox1.GetItemChecked(i))    {        MessageBox.Show(checkedListBox1.GetItemText(checkedListBox1.Items[i]));    }}

 

 

 

最近用到checklistbox控制項,在使用其過程中,花了較多的時間,這裡我收集了其相關的程式碼片段,希望對大家有所協助。1.添加項checkedListBox1.Items.Add(“藍色“); checkedListBox1.Items.Add(“紅色“); checkedListBox1.Items.Add(“黃色“); 2. 判斷第i項是否選中,選中為true,否則為falseif(checkedListBox1.GetItemChecked(i)){     return true;} else{     return false; } 3. 設定第i項是否選中checkedListBox1.SetItemChecked(i, true);  //true改為false為沒有選中。 4. 設定全選 添加一個名為select_all的checkbox控制項,由其控制checkedListBox是全選還是全不選。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);}} 5.得到全部選中的值 ,並將選中的項的文本組合成為一個字串。 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]);           }       }}  6. 設定CheckedListBox中第i項的Checked狀態checkedListBox1.SetItemCheckState(i, CheckState.Checked);  7. private void checkBoxAll_CheckedChanged(object sender, EventArgs e) {      if (checkBoxAll.Checked)      {          //被選擇了則將CheckedListBox中的所有條目都變為Checked狀態          for (int i = 0; i < checkedListBoxLayerControl.Items.Count;                   i++)          {     checkedListBoxLayerControl.SetItemCheckState(i,         CheckState.Checked); } }else {      //否則變成Unchecked狀態     for (int i = 0; i < checkedListBoxLayerControl.Items.Count; i++) {       checkedListBoxLayerControl.SetItemCheckState(i, CheckState.Unchecked); }              }}8. checkedListBox 單選設定(代碼實現)private void chkl_ItemAuditing_ItemCheck(object sender,   ItemCheckEventArgs e){      if (chkl_ItemAuditing.CheckedItems.Count > 0)     {          for (int i = 0; i < chkl_ItemAuditing.Items.Count; i++)          {         if (i != e.Index)          {              this.chkl_ItemAuditing.SetItemCheckState(i,              System.Windows.Forms.CheckState.Unchecked);          }     } } }9. checkedListBox1顯示一個資料庫中關鍵字對應的所有記錄for (int i = 0; i < table.Rows.Count; i++) {     string name = table.Rows["myname"].ToString();     string paw = table.Rows["mypaw"].ToString();     checkedListBox1.Items.Add(name + paw); } 10. for(i=0;i<CheckedListBox.Items.Count;i++)   {      if(CheckedListBox.GetItemText(CheckedListBox.Items)==“你得到的值“)   {         CheckedListBox.SetItemChecked(i,true);   }   } 11. 清除checkedListBox1中所有的選項for (int i = 0; i < checkedListBox1.Items.Count; i++){    checkedListBox1.Items.Clear();} 12. //設定索引為index的項為選中狀態for (int i = 0; i < checkedListBox1.Items.Count; i++) {    checkedListBox1.SetItemChecked(i, true);}  13.   for (int i = 0; i < checkedListBox1.Items.Count; i++) {          if (checkedListBox1.GetSelected(i))           {                    MessageBox.Show(checkedListBox1.CheckedItems.ToString());          }} 14.//選中checkedListBox1所有的選項 for (int i = 0; i < checkedListBox1.Items.Count; i++)         {          checkedListBox1.SetItemCheckState(i, CheckState.Checked);} 15.             for (int i = 0; i < checkedListBox1.Items.Count; i++) {            //如果checkedListBox1的第i項被選中,          //則顯示checkedListBox1對應的值          if (checkedListBox1.GetItemChecked(i))           {                MessageBox.Show(checkedListBox1.Items.ToString());           }} 16. //反向選擇checkedListBox1的選項for (int i = 0; i < checkedListBox1.Items.Count; i++) {     if (checkedListBox1.GetItemChecked(i))    {        checkedListBox1.SetItemChecked(i, false);    }    else    {        checkedListBox1.SetItemChecked(i, true);    } }17. //checkedListBox1中選定的項->checkedListBox2for (int i = 0; i < checkedListBox1.CheckedItems.Count; i++) {      checkedListBox2.Items.Add(this.checkedListBox1.CheckedItems); //remove是除去一個具體的值,不是index,注意了     this.checkedListBox1.Items.Remove(         this.checkedListBox1.CheckedItems);

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.