Sie sind auf Seite 1von 2

5/21/13 Visual Basic(Microsoft) -VB.

NET - Check all checkboxes in GroupBox

Smart questions
Smart answers
Smart people

Join
Directory
Search
Go Find A Forum Go
Tell A Friend
Whitepapers
Jobs

Home > Forums > Programmers > Languages > Visual Basic(Microsoft) -VB.NET Forum

Check all checkboxes in GroupBox


thread796-1501031
Share This

Read More Threads Like This One

johnisotank (TechnicalUser) 17
Sep
08
5:02
Hi,

I have a groupbox with 10 labels and 10 checkboxes.

I am trying to tick them all in one go but it keeps reporting the error:

Unable to cast object of type 'System.Windows.Forms.Label' to type 'System.Windows.Forms.CheckBox'.

I have another identical form with the same code and it works perfect so I don't understand whats wrong here..

CODE

For Each ctrl As CheckBox In MyGroupBox.Controls


ctrl.Checked = True
Next

could anyone advise pls?

thanks
John

mstrmage1768 (Programmer) 17
Sep
08
6:46
The problem is that you are looping through ALL the controls inside the GroupBox...and that includes what must be some Labels in the GroupBox. The
Labels cannot be cast as a CheckBox.

In this case, you need to loop through all the controls as generic controls, check to see if the control is a checkbox, cast it as a checkbox and finally check
it...

CODE

For Each ctrl As Control In Me.GroupBox1.Controls


If TypeOf ctrl Is CheckBox Then
DirectCast(ctrl, CheckBox).Checked = True
End If
Next

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And
Hobbs)

Robert L. Johnson III


CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
VB.NET Programmer

www.tek-tips.com/viewthread.cfm?qid=1501031 1/2
5/21/13 Visual Basic(Microsoft) -VB.NET - Check all checkboxes in GroupBox
rjoubert (Programmer) 17
Sep
08
6:52
I noticed you have 10 labels and 10 checkboxes. Are those labels strictly for the checkboxes? If so, the checkboxes have their own TEXT property that
you can use to "label" them, so you don't need the separate label controls.

johnisotank (TechnicalUser) 17
Sep
08
7:00
Thanks Robert thats working now.

I do actually have a similar form with the same layout of groupbox and it ticks all the boxes with no problem, even with labels in there.

Anyway, not going to let that worry me!

Thanks again
John

mstrmage1768 (Programmer) 17
Sep
08
7:08
Glad to help..... Did you see rjoubert's comment above???? That might be the difference.

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And
Hobbs)

Robert L. Johnson III


CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
VB.NET Programmer

johnisotank (TechnicalUser) 17
Sep
08
10:36
Hi rjoubert/Robert,

Yes, that explains it. I thought my other form had labels when in fact they just had TEXT assigned to the checkbox. This is why it wasn't erroring out -
because there were no labels!

Thanks a lot for the help

John

Read More Threads Like This One

Join | Jobs | Advertise | About Us | Contact Us | Site Policies

Copy right © 1998-2013 Tecumseh Group, Inc. A ll rights reserv ed.


Unauthorized reproduction or link ing forbidden without express written permission.

www.tek-tips.com/viewthread.cfm?qid=1501031 2/2

Das könnte Ihnen auch gefallen