The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Check all checkboxes in windows form C#
Discuss Check all checkboxes in windows form C# in the C Programming forum on Dev Shed. Check all checkboxes in windows form C# C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

June 26th, 2009, 10:31 AM
|
|
|
|
Check all checkboxes in windows form C#
I have a windows form with something like 20 checkboxes. I have a radio button above that when set one way will check all the boxes and set another way will uncheck them all.
My question is: is there a way to traverse through all the checkboxes on the form and check them in a loop? I don't want to have to manually do it for every checkbox. Thanks
|

June 26th, 2009, 10:32 AM
|
|
|
|
Get data from new form? C#
I have my main form which launches a new form with a button click. In that new form, a user can select a bunch of options and then hit "continue" button. The button closes that second form and is supposed to give all that data back to the first form. I know it's possible I just don't have a lot of experience with C#. any help? Thanks
|

June 26th, 2009, 12:31 PM
|
|
Closet coder
|
|
Join Date: Feb 2005
Location: Plantation, FL <---south florida
|
|
Quote: | Originally Posted by Optimum I have a windows form with something like 20 checkboxes. I have a radio button above that when set one way will check all the boxes and set another way will uncheck them all.
My question is: is there a way to traverse through all the checkboxes on the form and check them in a loop? I don't want to have to manually do it for every checkbox. Thanks |
you can try something along the lines of (not tested)
c# Code:
Original
- c# Code |
|
|
|
foreach(CheckBox chk in form.Controls) { chk.Checked = true; }
Quote: | I have my main form which launches a new form with a button click. In that new form, a user can select a bunch of options and then hit "continue" button. The button closes that second form and is supposed to give all that data back to the first form. I know it's possible I just don't have a lot of experience with C#. any help? Thanks |
you can have a method in the "popup" form call a method in the original form that sets data from your "popup" before you close it.
__________________
|

June 26th, 2009, 01:37 PM
|
|
|
|
there doesn't seem to be a "Controls" member for the form. The second answer is good tho - thanks
|

June 26th, 2009, 02:06 PM
|
|
|
|
Actually how can I even access methods from the first form in the second form? I tried passing the form1 object but it doesn't work
|

June 26th, 2009, 02:28 PM
|
|
|
Quote: | Originally Posted by Optimum Actually how can I even access methods from the first form in the second form? I tried passing the form1 object but it doesn't work |
The methods have to be declared public.
|

June 26th, 2009, 02:51 PM
|
|
Closet coder
|
|
Join Date: Feb 2005
Location: Plantation, FL <---south florida
|
|
Quote: | Originally Posted by Optimum there doesn't seem to be a "Controls" member for the form. The second answer is good tho - thanks |
are your checkboxes in some container on the form? (like a groupbox or a panel or tabcontrol or such?)
|

June 26th, 2009, 03:05 PM
|
|
|
|
I know the method must be declared public - but it still doesn't pick up the method.
The checkboxes are in the same groupbox yes.
|

June 26th, 2009, 03:10 PM
|
 |
Bellevue WA, USA
|
|
Join Date: May 2004
Location: Bellevue Washington, USA
|
|
|
You should not make a form dependent on a parent form's interfaces. This is called a circular dependency and should be avoided. It's bad design because it will make your program brittle and creates major obstacles to code reuse.
The OP's question is more of a .NET/WinForms question than a C# language issue they might find quicker more accurate responses for in the .NET forum.
I'd have to go look it up at MSDN, but I seem to recall there are multiple standard means for passing data between forms. One involves having the parent query the child for it's state before discarding it and the other involves passing some data (usually a pointer/reference) to your constructor that it would save and later use to write back the data.
In any case, you should at least have some intermediary abstraction for the data representing the check-box states. Then, one day if you ever need to reuse that form from some other, all it has to know about are the two data types; the form and the intermediary. There are more than one design pattern available to achieve this. Probably the simplest one would be a wrapper class around your form so all the details of interaction would be completely hidden from the parent.
__________________
My worst nightmare was a pointless infinite loop.
Work in progress; don't poke the curmudgeon!
http://www.odonahue.com/
|

June 26th, 2009, 10:12 PM
|
|
|
Quote: | Originally Posted by jwdonahue You should not make a form dependent on a parent form's interfaces. This is called a circular dependency and should be avoided. It's bad design because it will make your program brittle and creates major obstacles to code reuse.
|
Agreed.
But I don't think he is anywhere near ready to process this information.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|