Why selected items in a checkedListBox don't show in order
-
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
-
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
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 -
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
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