Click here to Skip to main content
15,997,960 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello guys,


Here is my code:

C#
CheckBox[] cb = new CheckBox[100];
for (int i = 1; r2.Read(); i++)
            {
                int height = 1;
                    cb[i] = new CheckBox();
                    cb[i].Name = i.ToString();
                    cb[i].Text = r2[1].ToString();
                    cb[i].Location = new Point(30, i * 30);
                    this.Controls.Add(cb[i]);
                    height += 22;
            }


I also have a button event, when i clicked that button, i want to get the text data which is belong to that selected checkbox. I really need your help. thank you

~Cansin
Posted

There are a few issues there:

1. The array index starts at 0, not 1

2. You don't check for array overflow, what if the for loop crosses 100 iterations?

3. What's height used for? It's local to the for block and not used for anything

4. Define "selected"? You can have a checkbox in focus, but the moment you click on the button the checkbox loses focus. So at that time there is no "selected" checkbox, so to speak.
 
Share this answer
 
Comments
cansin.durdu 8-Feb-11 10:16am    
Thank you for your warnings. The code which i mentioned on the top was the most closest to my code.
I know arrays always start from 0 but as you see the Location of checkboxes is a little bit belong to start index of array. Well 100 is just for a try. For loop never crosses 100 iterations cuz i know how much it will be :) Anyway thank you for your advices ;) next time i'll keep them in my mind.
Sergey Alexandrovich Kryukov 8-Feb-11 16:14pm    
That's all valid, my 5.
I prefer John's answer, anyway.
--SA
Why don't you use a Listbox instead? You can turn on checkboxes, and when you select an item, just get the item's text. Beyond that, it will look a lot better than having 100 individual listboxes on a form.
 
Share this answer
 
Comments
cansin.durdu 8-Feb-11 10:09am    
thanks a bunch! This was an alternative way and i think the most troubleless :) Just tried and working perfectly.
#realJSOP 8-Feb-11 10:17am    
Mark my asnwer as the answer then. :)
Nish Nishant 8-Feb-11 10:10am    
Voted 5, proposed as answer.

Instead of a ListBox, I'd use CheckedListBox directly.
Sergey Alexandrovich Kryukov 8-Feb-11 16:13pm    
Good point, my 5. This is yet another weird artificial idea in question. If you answered, it wouldn't help much, would make only worse.
--SA
An alternative approach would be to use a CheckedListBox:

http://msdn.microsoft.com/en-us/library/system.windows.forms.checkedlistbox.aspx[^]
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 8-Feb-11 16:15pm    
Maybe this is exactly what should be used, my 5.
No way a regular check box where its name matter, such a lousy idea!
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900