Accessing groupboxes in a form

F

Frank

I have several groupboxes in a form, and I need to go
through them to check some things. Thus I have a Sub like
this:

Dim grpbox_temp As GroupBox
Dim cbox_temp As CheckBox

For Each grpbox_temp In Me.Controls
...

Now, I can compile this, but when I run it, I get an
error in the For Each statement: "The specified change is
invalid" (tranlated)

What am I doing wrong here?


Regards,

Frank
 
C

Cor

Hi Frank
Dim grpbox_temp As GroupBox
Dim cbox_temp As CheckBox

For Each grpbox_temp In Me.Controls
...
\\\
dim ctr as control
for each ctr in groupbox1.controls
if typeof ctr is checkbox then
dim chk as checkbox = directcast(ctr,checkbox)
chk.xxxxxxxx = what you want
end if
next
////
roughly typed, dont look at typos.

I think this is something you are looking for?t
Cor
 
A

Armin Zingler

Frank said:
I have several groupboxes in a form, and I need to go
through them to check some things. Thus I have a Sub like
this:

Dim grpbox_temp As GroupBox
Dim cbox_temp As CheckBox

For Each grpbox_temp In Me.Controls
...

Now, I can compile this, but when I run it, I get an
error in the For Each statement: "The specified change is
invalid" (tranlated)

What am I doing wrong here?


dim c as object

For Each c In Me.Controls
if typeof c is Groupbox then
Dim grpbox_temp As GroupBox
grpbox_temp = directcast(c, groupbox)
'access groupbox here
end if
next

As Me.Controls contains _all_ controls directly placed on the Form, you get
an error whenever an attempt is made to assign a control to grpbox_temp
that is not a group box.
 
K

Ken Tucker [MVP]

Hi,

Try this.
Dim ctrl As Control

For Each ctrl In Me.Controls

If TypeOf ctrl Is GroupBox Then

Dim gb As GroupBox = ctrl

Debug.WriteLine(gb.Name)

End If

Next

Ken
 
W

What-a-Tool

Each groupbox has it's own controls collection, so you have to check each
individually, I do believe.

Dim ctrl As Control
For Each ctrl in grpBox_Temp.Controls

Or if you have a bunch of groupboxes, I suppose you could loop through
Me.Controls to find the Groupboxes, and then go through each GroupBox in a
nested For Each Loop

--

/ Sean the Mc /


"I have not failed. I've just found 10,000 ways that won't work."
- Thomas Alva Edison (1847-1931)
 
R

RR SPSCC

The controls collection holds ALL controls so the code 'For Each grpbox_temp
In Me.Controls' crashes. What you need is:

dim ctl as control 'need generic control since that's what's in controls
collection
for each ctl in me.controls
if typeof ctl is groupbox 'this lets you look for specific types of
controls
dim ctl2 as control ' this is to reference the controls in the
groupbox controls collection
for each ctl2 in ctl.controls ' this is the collection in the
groupbox
'now you can work with the controls in your groupbox
next
end if
next

HTH
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top