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

11/26/2014 6:04:52 PM #

Sheryl

It's really a cool and useful piece of information. I am glad that you simply shared this helpful info with us. Please keep us up to date like this. Thank you for sharing.

Also visit my blog post - school [ Sheryl - http://bit.ly/1wKsPiV ]

Sheryl United States | Reply

1/25/2015 9:14:33 PM #

Lauri

No matter if some one searches for his required thing, thus he/she wishes to be available that in detail, thus that thing is maintained over here.

Here is my webpage ... advertising ( Lauri - http://dentalpracticesstaff.tumblr.com/ )

Lauri United States | Reply

5/4/2015 4:01:00 AM #

get rid of dark underarms

I am regular visitor, how are you everybody? This paragraph posted at this site is in fact fastidious.

get rid of dark underarms United States | Reply

5/4/2015 11:01:19 AM #

underarm whitening products

Greetings I am so grateful I found your website, I really found you by error, while I was browsing on Google for something else, Anyhow I am here now and would just like to say cheers for a marvelous post and a all round thrilling blog (I also love the theme/design), I don't have time to browse it all at the minute but I have saved it and also added in your RSS feeds, so when I have time I will be back to read much more, Please do keep up the superb job.

underarm whitening products United States | Reply

5/5/2015 7:35:29 AM #

armpit bleaching cream

Good day! I could have sworn I've been to this site before but after browsing through some of the post I realized it's new to me. Nonetheless, I'm definitely happy I found it and I'll be book-marking and checking back often!

armpit bleaching cream United States | Reply

5/5/2015 11:08:39 AM #

how to whiten underarms fast

You made some decent points there. I checked on the web to find out more about the issue and found most people will go along with your views on this website.

how to whiten underarms fast United States | Reply

5/14/2015 12:19:57 PM #

http://www.viagrabelgiquefr.com/vente-viagra-libre

Definitely consider that that you said. Your favorite reason seemed to be at the web the simplest factor to understand of. I say to you, I definitely get irked even as other people consider concerns that they plainly don't recognise about. You managed to hit the nail upon the highest as neatly as outlined out the entire thing without having side effect , people can take a signal. Will likely be again to get more. Thank you

http://www.viagrabelgiquefr.com/vente-viagra-libre United States | Reply

5/15/2015 2:41:20 AM #

Wonder Clicks

Hi! This post could not be written any better! Reading through this post reminds me of my old room mate! He always kept talking about this. I will forward this article to him. Pretty sure he will have a good read. Thank you for sharing!

Here is my homepage:  Wonder Clicks - www.foaie.com/.../...-clicks-review-and-bonus.html

Wonder Clicks United States | Reply

5/18/2015 2:40:19 PM #

how to lighten underarms

Have you ever considered about including a little bit more than just your articles? I mean, what you say is important and everything. But just imagine if you added some great pictures or video clips to give your posts more, "pop"! Your content is excellent but with pics and video clips, this website could certainly be one of the best in its field. Amazing blog!

how to lighten underarms United States | Reply

5/29/2015 4:21:38 PM #

how to whiten armpit

Hello there! I know this is kinda off topic but I was wondering which blog platform are you using for this site? I'm getting tired of Wordpress because I've had issues with hackers and I'm looking at alternatives for another platform. I would be awesome if you could point me in the direction of a good platform.

how to whiten armpit United States | Reply

5/30/2015 6:14:17 PM #

luxury home

Does your website have a contact page? I'm having a tough time locating it but, I'd like to shoot you an e-mail. I've got some recommendations for your blog you might be interested in hearing. Either way, great site and I look forward to seeing it expand over time.

luxury home United States | Reply

5/30/2015 7:09:32 PM #

casino bonus money

Hey there! Someone in my Myspace group shared this site with us so I came to give it a look. I'm definitely loving the information. I'm bookmarking and will be tweeting this to my followers! Excellent blog and terrific style and design.

casino bonus money United States | Reply

6/1/2015 1:50:05 AM #

amzn.to

I know this if off topic but I'm looking into starting my own blog and was wondering what all is required to get setup? I'm assuming having a blog like yours would cost a pretty penny? I'm not very web savvy so I'm not 100% certain. Any suggestions or advice would be greatly appreciated. Kudos

amzn.to United States | Reply

6/2/2015 6:13:23 PM #

good costume ideas

You are so awesome! I do not suppose I've read something like that before. So nice to find someone with a few genuine thoughts on this subject. Seriously.. many thanks for starting this up. This web site is one thing that's needed on the web, someone with a little originality!

good costume ideas United States | Reply

Add comment


(Will show your Gravatar icon)

biuquote
  • Comment
  • Preview
Loading



Copyright © 2009 APJ Software

最新评论

Comment RSS

公告

欢迎使用APJ Blog!

日历

<<  July 2015  >>
MoTuWeThFrSaSu
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789

View posts in large calendar