C# CheckedListBox控件的用法

by jack 11. August 2009 19:56

最近用到checklistbox控件,在使用其过程中,花了较多的时间,这里我收集了其相关的代码段,希望对大家有所帮助。

1.

添加项


checkedListBox1.Items.Add("
蓝色");
checkedListBox1.Items.Add("
红色");
checkedListBox1.Items.Add("
黄色");

 

2.
判断第i项是否选中,选中为true,否则为false


if
checkedListBox1.GetItemChecked(i)

{

     return true;

}

else

{

     return false;

}

 

3.
设置第i项是否选中

checkedListBox1.SetItemChecked(i, true);  //true
改为false为没有选中。

 

4.
设置全选
添加一个名为select_allcheckbox控件,由其控制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中选定的项->checkedListBox2


for (int i = 0; i < checkedListBox1.CheckedItems.Count; i++)
{
     checkedListBox2.Items.Add(this.checkedListBox1.CheckedItems);

 

//remove是除去一个具体的值,不是index,注意了
     this.checkedListBox1.Items.Remove(

         this.checkedListBox1.CheckedItems);      
}

Tags:

技术文章 | 技术文章 | 技术文章

Comments

6/24/2013 6:19:37 AM #

veranda ouverte

prix des verandas ils prix veranda il a veranda ancienne tu veranda occasion kit veranda  eclairage veranda - http://www.verandafrance.com  veranda acier en écoutant véranda aluminium véranda bois veranda ancienne

veranda ouverte | Reply

8/20/2013 11:07:09 AM #

SEO

I like to disseminate knowledge that will I have built up with the year to assist improve team efficiency.

Also visit my weblog:  SEO - http://seo.market11-tw.org/?page_id=582

SEO | Reply

8/20/2013 12:44:53 PM #

binary options trading

What Is a good 35mm Move Scanner

my homepage ::  binary options trading - http://www.boptions2013.co.uk

binary options trading | Reply

8/20/2013 2:47:37 PM #

http://www.keywordseo.com.tw

Hi there! I just would like to give an enormous thumbs up for the nice data you��ve here on this post. I will probably be coming back to your blog for extra soon.

Feel free to surf to my homepage; SEO [ http://www.keywordseo.com.tw" rel="nofollow">http://www.keywordseo.com.tw - http://www.keywordseo.com.tw" rel="nofollow">http://www.keywordseo.com.tw ]

http://www.keywordseo.com.tw | Reply

8/20/2013 6:51:55 PM #

free online background check

It's amazing in support of me to have a web site, which is good for my experience. thanks admin

Review my web site -  free online background check - http://rbmbanjarnegara.com/guestbook/

free online background check | Reply

8/20/2013 6:52:22 PM #

free online background check

It's amazing in support of me to have a web site, which is good for my experience. thanks admin

Review my web site -  free online background check - http://rbmbanjarnegara.com/guestbook/

free online background check | Reply

8/20/2013 8:15:24 PM #

cheap london escorts

Hey, I think your website might be having browser compatibility issues. When I look at your blog site in Ie, it looks fine but when opening in Internet Explorer, it has some overlapping. I just wanted to give you a quick heads up! Other then that, fantastic blog!

Also visit my page  cheap london escorts - http://www.outcallescorts4u.com

cheap london escorts | Reply

8/21/2013 6:42:36 AM #

Cheap London Kensington Escorts

Saved as a favorite, I like your web site!

my web page:  Cheap London Kensington Escorts - dlondonescorts.co.uk/...ington-london-escorts.html

Cheap London Kensington Escorts | Reply

8/21/2013 2:56:59 PM #

super real

This type of loan emerges for the basis of the home equity  super real - http://www.to3tfake2.com  to get big work from home opportunities, financial sources like capital international fund are needed.

super real | Reply

8/22/2013 4:22:03 PM #

如何推廣网站

I seriously love your site.. Pleasant colors & theme. Did you develop this amazing site yourself? Please reply back as I'm looking to create my very own website and would love to find out where you got this from or exactly what the theme is named. Appreciate it!

Here is my website;  如何推廣网站 - http://www.ringoli.net

如何推廣网站 | Reply

8/23/2013 9:17:44 PM #

link

that is really cool that is really cool errtwehj yeah really awesome i say....... this is a comment you know please could you maybe visit my website for further info you know

Feel free to visit my web-site -  link - www.snowandrock.com/insoles/run/fcp-category/list

link | Reply

8/23/2013 9:51:26 PM #

link

that is really cool that is really cool errtwehj yeah really awesome i say....... this is a comment you know please could you maybe visit my website for further info you know

My web-site ...  link - www.shuropody.com/.../insoles.html

link | Reply

8/24/2013 10:48:59 AM #

procurve

What's up friends, how is all, and what you desire to say about this piece of writing, in my view its really amazing for me.

Look at my site ::  procurve - http://www.procurve.com.hk/

procurve | Reply

8/25/2013 5:29:38 AM #

free iphone

What's up, I want to subscribe for this weblog to take newest updates, so where can i do it please help.

Also visit my site -  free iphone - http://www.youtube.com/watch?v=FUFDiKhrEo8

free iphone | Reply

8/26/2013 3:40:06 AM #

Z4FodG

937479 96035Fantastic post will probably be linking this on a couple of websites of mine keep up the great work. 683140

Z4FodG | Reply

8/26/2013 5:31:38 AM #

voyance gratuite

Voyantissime voyance voyance en direct gratuit

My website:  voyance gratuite - http://www.foryou-wasserbett.com

voyance gratuite | Reply

8/26/2013 5:31:55 AM #

gps pour moto

I just like the helpful info you provide in your articles. I will bookmark your blog and take a look at again here frequently. I am quite sure I'll learn lots of new stuff right right here! Best of luck for the next!

Here is my web blog -  gps pour moto - http://www.gpsmoto.org/

gps pour moto | Reply

8/26/2013 6:12:46 AM #

escorts in london

My coder is trying to persuade me to move to .net from PHP. I have always disliked the idea because of the costs. But he's tryiong none the less. I've been using WordPress on numerous websites for about a year and am anxious about switching to another platform. I have heard excellent things about blogengine.net. Is there a way I can transfer all my wordpress content into it? Any kind of help would be really appreciated!

Here is my page ...  escorts in london - http://www.tlondonescorts.co.uk

escorts in london | Reply

8/26/2013 8:53:49 PM #

Chandra

Please let me know if you're looking for a article writer for your weblog. You have some really great posts and I think I would be a good asset. If you ever want to take some of the load off, I'd love to write some material for your blog in exchange for a link back to mine. Please blast me an e-mail if interested. Kudos!

My webpage Capture His Heart -  Chandra - http://CaptureHisHeart--Reviews.blogspot.com  -

Chandra | Reply

8/27/2013 9:57:30 PM #

sklep z bielizn¹ online

Just wish to say your article is as amazing. The clearness in your put up is simply nice and i can assume you are knowledgeable in this subject. Well along with your permission let me to snatch your feed to stay updated with imminent post. Thank you 1,000,000 and please continue the gratifying work.

my site;  sklep z bielizn¹ online - http://domki-w-dolinie.pooh.pl/ponczochy/

sklep z bielizn¹ online | Reply

8/27/2013 10:05:34 PM #

bielizna satynowa

Touche. Outstanding arguments. Keep up the great effort.

my site ...  bielizna satynowa - figura-ladna.pooh.pl/sexowna-bielizna-damska/

bielizna satynowa | Reply

10/31/2013 10:41:38 PM #

adventure travel

Good info. Lucky me I ran across your site by accident (stumbleupon). I've saved it for later!

Look into my web page;  adventure travel - http://offshoreagile.com/

adventure travel | Reply

11/1/2013 1:41:46 AM #

Numerologist.com

Howdy just wanted to give you a quick heads up and let you know a few of the pictures aren't loading correctly. I'm not sure why but I think its a linking issue. I've tried it in two different browsers and both show the same results.

My web blog  Numerologist.com - www.petisud.com/.../...logistcom-numerologist.html

Numerologist.com | Reply

11/1/2013 8:24:57 AM #

international removals

By default, online gambling is illegal, which means operating online casinos is also part of the illegal activity. It is also suggested to peruse the Internet to read the reviews, commentaries and testimonies of online casino gamers like you. You can either play for fun, or play for real money. From booking  a movie  ticket  to booking  a place for marriage ,  each particular thing can be done  over the internet and so as gambling.

Also visit my blog;  international removals - http://www.stgeorgestransport.com

international removals | Reply

11/1/2013 9:26:53 AM #

Lazaro

Hello There. I found your weblog using msn. This is an extremely well written article. I’ll make sure to bookmark it and

come back to read more of your useful information. Thanks for the

post. I’ll definitely comeback.

Feel free to surf to my weblog :: Dolores Costa Blanca ( Lazaro - kevinsonlinemarketing.com/.../article.php?id=17010 )

Lazaro | Reply

4/9/2014 9:37:41 PM #

Rebbeca

I have been browsing online more than 4 hours today, yet I never found any interesting article like yours. It's pretty worth enough for me. In my opinion, if all site owners and bloggers made good content as you did, the net will be a lot more useful than ever before.

Look into my blog post :: flower delivery winston salem ( Rebbeca - http://localflowersdelivery.wordpress.com/ )

Rebbeca United States | Reply

4/11/2014 6:24:49 AM #

Alexander

Hi mates, how is all, and what you wish for to say on the topic of this piece of writing, in my view its truly remarkable designed for me.

Feel free to visit my weblog; mold removal services los angeles ( Alexander - http://showermoldsremoval.webs.com/ )

Alexander United States | Reply

4/11/2014 8:43:23 AM #

Ralph Lauren Kids

I just like the valuable info you provide for your articles. I'll bookmark your weblog and check again right here frequently. I am relatively sure I will be informed a lot of new stuff right right here! Best of luck for the next!

My homepage ...  Ralph Lauren Kids - www.kwikheat.co.uk/cheap-ralph-lauren-kids.html

Ralph Lauren Kids United States | Reply

Add comment


(Will show your Gravatar icon)

biuquote
  • Comment
  • Preview
Loading



Copyright © 2009 APJ Software

最新评论

Comment RSS

公告

欢迎使用APJ Blog!

日历

<<  November 2014  >>
MoTuWeThFrSaSu
272829303112
3456789
10111213141516
17181920212223
24252627282930
1234567

View posts in large calendar