最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C# CheckedListBox控件的用法總結(jié)

 更新時(shí)間:2016年12月03日 15:12:30   作者:麥田怪人  
本篇文章主要介紹了C# CheckedListBox控件的用法總結(jié),想要學(xué)習(xí)CheckedListBox的同學(xué)可以了解一下。

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

其實(shí)這里遍歷的只是高亮的值并不是打勾的值。遍歷打勾的值要用下面的代碼:

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

最近用到checklistbox控件,在使用其過(guò)程中,花了較多的時(shí)間,這里我收集了其相關(guān)的代碼段,希望對(duì)大家有所幫助。

1.添加項(xiàng)

checkedListBox1.Items.Add(“藍(lán)色“); 
checkedListBox1.Items.Add(“紅色“); 
checkedListBox1.Items.Add(“黃色“);

2. 判斷第i項(xiàng)是否選中,選中為true,否則為false

if(checkedListBox1.GetItemChecked(i))
{
   return true;
} 
else
{
   return false; 
}

3. 設(shè)置第i項(xiàng)是否選中

checkedListBox1.SetItemChecked(i, true); //true改為false為沒(méi)有選中。

4. 設(shè)置全選

添加一個(gè)名為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.得到全部選中的值 ,并將選中的項(xiàng)的文本組合成為一個(gè)字符串。

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. 設(shè)置CheckedListBox中第i項(xiàng)的Checked狀態(tài)

checkedListBox1.SetItemCheckState(i, CheckState.Checked);

7.

private void checkBoxAll_CheckedChanged(object sender, EventArgs e) 
{ 
   if (checkBoxAll.Checked) 
   { 
     //被選擇了則將CheckedListBox中的所有條目都變?yōu)镃hecked狀態(tài) 
     for (int i = 0; i < checkedListBoxLayerControl.Items.Count;
          i++) 
     {   
checkedListBoxLayerControl.SetItemCheckState(i, 
    CheckState.Checked); 
} 
}
else 
{ 
   //否則變成Unchecked狀態(tài) 
  for (int i = 0;
 i < checkedListBoxLayerControl.Items.Count; i++) 
{
    checkedListBoxLayerControl.SetItemCheckState(i, CheckState.Unchecked); 
}       
}
}

8. checkedListBox 單選設(shè)置(代碼實(shí)現(xiàn))

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顯示一個(gè)數(shù)據(jù)庫(kù)中關(guān)鍵字對(duì)應(yīng)的所有記錄

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中所有的選項(xiàng)

for (int i = 0; i < checkedListBox1.Items.Count; i++)
{
  checkedListBox1.Items.Clear();
}

12. 

//設(shè)置索引為index的項(xiàng)為選中狀態(tài)
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所有的選項(xiàng)
 
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項(xiàng)被選中,
     //則顯示checkedListBox1對(duì)應(yīng)的值
     if (checkedListBox1.GetItemChecked(i)) 
     { 
        MessageBox.Show(checkedListBox1.Items.ToString(
)); 
     }
}

16.

//反向選擇checkedListBox1的選項(xiàng)
for (int i = 0; i < checkedListBox1.Items.Count; i++) 
{ 
  if (checkedListBox1.GetItemChecked(i)) 
  { 
    checkedListBox1.SetItemChecked(i, false); 
  } 
  else 
  { 
    checkedListBox1.SetItemChecked(i, true); 
  } 
}

17.

//checkedListBox1中選定的項(xiàng)->checkedListBox2
for (int i = 0; i < checkedListBox1.CheckedItems.Count; i++) 
{ 
   checkedListBox2.Items.Add(this.checkedListBox1.CheckedItems);
 
//remove是除去一個(gè)具體的值,不是index,注意了
   this.checkedListBox1.Items.Remove(
     this.checkedListBox1.CheckedItems);

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • C#實(shí)現(xiàn)帶陰歷顯示的日期代碼

    C#實(shí)現(xiàn)帶陰歷顯示的日期代碼

    這篇文章主要介紹了C#實(shí)現(xiàn)帶陰歷顯示的日期代碼,可以顯示節(jié)假日及農(nóng)歷日期,在C#項(xiàng)目開(kāi)發(fā)中非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2014-10-10
  • 一篇文章徹底搞清楚c#中的委托與事件

    一篇文章徹底搞清楚c#中的委托與事件

    這篇文章主要給大家介紹了如何通過(guò)一篇文章徹底搞清楚c#中的委托與事件,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用c#具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • Linq利用Distinct去除重復(fù)項(xiàng)問(wèn)題(可自己指定)

    Linq利用Distinct去除重復(fù)項(xiàng)問(wèn)題(可自己指定)

    這篇文章主要介紹了Linq利用Distinct去除重復(fù)項(xiàng)問(wèn)題(可自己指定),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-01-01
  • 詳解如何獲取C#類(lèi)中發(fā)生數(shù)據(jù)變化的屬性信息

    詳解如何獲取C#類(lèi)中發(fā)生數(shù)據(jù)變化的屬性信息

    這篇文章主要介紹了詳解如何獲取C#類(lèi)中發(fā)生數(shù)據(jù)變化的屬性信息,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-05-05
  • C#同步、異步遠(yuǎn)程下載文件實(shí)例

    C#同步、異步遠(yuǎn)程下載文件實(shí)例

    使用C#下載一個(gè)Internet上的文件主要是依靠HttpWebRequest/HttpWebResonse和WebClient。具體處理起來(lái)還有同步和異步兩種方式,所以我們其實(shí)有四種組合
    2014-04-04
  • C#留言時(shí)間格式化

    C#留言時(shí)間格式化

    本文給大家分享的是仿微博或者空間中,發(fā)布內(nèi)容之后提示NN秒之前留言的代碼,主要是通過(guò)發(fā)布時(shí)間和當(dāng)前時(shí)間直接的差值來(lái)計(jì)算,十分的簡(jiǎn)單實(shí)用,有需要的小伙伴可以參考下。
    2015-05-05
  • C#實(shí)現(xiàn)寫(xiě)系統(tǒng)日志的方法

    C#實(shí)現(xiàn)寫(xiě)系統(tǒng)日志的方法

    這篇文章主要介紹了C#實(shí)現(xiàn)寫(xiě)系統(tǒng)日志的方法,涉及C#針對(duì)系統(tǒng)日志的創(chuàng)建、寫(xiě)入及刪除等技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2015-08-08
  • 最新評(píng)論

    新田县| 两当县| 自贡市| 岳池县| 常熟市| 威信县| 响水县| 云阳县| 武隆县| 凤凰县| 安徽省| 孝感市| 株洲县| 留坝县| 昭平县| 大连市| 谷城县| 阜新市| 宜良县| 靖宇县| 辰溪县| 九龙城区| 措勤县| 年辖:市辖区| 陕西省| 祁门县| 双桥区| 铁力市| 盐津县| 红原县| 弥勒县| 博湖县| 义乌市| 利津县| 永靖县| 华池县| 昂仁县| 会理县| 武鸣县| 宜丰县| 明光市|