Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Why selected items in a checkedListBox don't show in order

Why selected items in a checkedListBox don't show in order

Scheduled Pinned Locked Moved C#
questiondatabasehelp
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    abhishk2001 yahoo com
    wrote on last edited by
    #1

    Hi, I have 5 CheckedListBox controls on a winForm. I am appending the selected items in a stringBuilder on a buttonClick Event. my question is, say, I have checkedListBox named as A, B, C, D and E. after selecting the items from each CheckedListBox, it first shows me the selected values from E,D,C,B and A. why does it shows the selection in reverse, when i am adding the selected values immediately to the StringBuilder. my code is given below. Thanks, for ur help. I am actually using the selected items to form a dynamic query. StringBuffer sb=new StringBuffer(); CheckedListBox clb = null; foreach(Control c in groupBox1.Controls) { if(c is CheckedListBox) { clb = c as CheckedListBox; if(clb.CheckedItems.Count !=0) { for(int i=0; i<=clb.CheckedItems.Count -1; i++) { sb.Append(clb.CheckedItems[i].ToString()); sb.Append(", "); } } } MessageBox.Show(sb.ToString()); abhi

    B S 2 Replies Last reply
    0
    • A abhishk2001 yahoo com

      Hi, I have 5 CheckedListBox controls on a winForm. I am appending the selected items in a stringBuilder on a buttonClick Event. my question is, say, I have checkedListBox named as A, B, C, D and E. after selecting the items from each CheckedListBox, it first shows me the selected values from E,D,C,B and A. why does it shows the selection in reverse, when i am adding the selected values immediately to the StringBuilder. my code is given below. Thanks, for ur help. I am actually using the selected items to form a dynamic query. StringBuffer sb=new StringBuffer(); CheckedListBox clb = null; foreach(Control c in groupBox1.Controls) { if(c is CheckedListBox) { clb = c as CheckedListBox; if(clb.CheckedItems.Count !=0) { for(int i=0; i<=clb.CheckedItems.Count -1; i++) { sb.Append(clb.CheckedItems[i].ToString()); sb.Append(", "); } } } MessageBox.Show(sb.ToString()); abhi

      B Offline
      B Offline
      Bill Dean
      wrote on last edited by
      #2

      Hi Abhi, Take a look in your form's InitializeComponent() method. Somewhere in there, the VS designer wrote you some code like:groupBox1.Controls.Add(this.CheckedListBoxE); groupBox1.Controls.Add(this.CheckedListBoxD); ...
      The items appear in the Controls collection in the order in which they were added (AFAIK). So either: edit the InitializeComponent() code to reverse the order in which they are added, or just reverse your loop and work for the end of the collection forward. I'd do the former, but I don't think there's a compelling argument either way. Hope this helps, Bill

      1 Reply Last reply
      0
      • A abhishk2001 yahoo com

        Hi, I have 5 CheckedListBox controls on a winForm. I am appending the selected items in a stringBuilder on a buttonClick Event. my question is, say, I have checkedListBox named as A, B, C, D and E. after selecting the items from each CheckedListBox, it first shows me the selected values from E,D,C,B and A. why does it shows the selection in reverse, when i am adding the selected values immediately to the StringBuilder. my code is given below. Thanks, for ur help. I am actually using the selected items to form a dynamic query. StringBuffer sb=new StringBuffer(); CheckedListBox clb = null; foreach(Control c in groupBox1.Controls) { if(c is CheckedListBox) { clb = c as CheckedListBox; if(clb.CheckedItems.Count !=0) { for(int i=0; i<=clb.CheckedItems.Count -1; i++) { sb.Append(clb.CheckedItems[i].ToString()); sb.Append(", "); } } } MessageBox.Show(sb.ToString()); abhi

        S Offline
        S Offline
        S Sansanwal
        wrote on last edited by
        #3

        Because .NET have added them to group in that order. Look into InitializeComponent method You would see something like // // groupBox1 // this.groupBox1.Controls.Add(this.checkedListBox4); this.groupBox1.Controls.Add(this.checkedListBox3); this.groupBox1.Controls.Add(this.checkedListBox2); this.groupBox1.Controls.Add(this.checkedListBox1); Sanjay Sansanwal www.sansanwal.com

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups