Loop through certain controls

K

Kevin L

I have the following loop:

Dim objPanel As Panel

For Each objPanel In Me.Controls

'do something here

Next



I receive an error when my loop encounters a control that is not a Panel
control.

I only want to perform actions on the Panel controls on my form and skip any
other type of control.

Is there a way to loop only through the Panel controls?
 
M

Mattias Sjögren

Kevin,
Is there a way to loop only through the Panel controls?

For Each ctl As Control In Me.Controls
If TypeOf ctl Is Panel Then
objPanel = CType(ctl, Panel)
'do something here
End If
Next



Mattias
 
J

Jan Tielens

Dim objPanel As Object

For Each objPanel In Me.Controls
If objPanel.GetType Is GetType(Panel) Then
'do something here
End If
Next
 
B

Brian W

You will have to do something like this (goiung from memory)


Dim objPanel As Control

For Each objPanel In Me.Controls

If objPanel Is Panel Then
'do something here
End If

Next

HTH
Brian W
 
H

Herfried K. Wagner [MVP]

M

Mattias Sjögren

Wow! 3 different ways, each just as correct... that was cool. =)

Nope, only two correct, one doesn't compile. Out of the two working
ones, one works also for types derived from Panel and the other does
not.



Mattias
 
H

Herfried K. Wagner [MVP]

* Mattias Sjögren said:
For Each ctl As Control In Me.Controls
If TypeOf ctl Is Panel Then
objPanel = CType(ctl, Panel)

Why not use 'DirectCast' here?
 
M

Mattias Sjögren

Herfried,
Why not use 'DirectCast' here?

5 less characters to type?! :) Since it produces the same code in
this context, I suppose it's a matter of style and personal preference
which one you choose.



Mattias
 
B

Brian W

OK, OK, I forgot the TypeOf (Doh!)

BW



Brian W said:
You will have to do something like this (goiung from memory)


Dim objPanel As Control

For Each objPanel In Me.Controls

If objPanel Is Panel Then
'do something here
End If

Next

HTH
Brian W
 
C

Cor

Hi Mattias,

I do not understand what you mean
Nope, only two correct, one doesn't compile. Out of the two working
ones, one works also for types derived from Panel and the other does
not.

I could understand it if it was,

Nope only two correct, one has a typing error.
The two with casting also work for types originaly derived from Control as a
Panel and the others does not.

Cor
 
C

CJ Taylor

I hate all of you.


Cor said:
Hi Mattias,

I do not understand what you mean


I could understand it if it was,

Nope only two correct, one has a typing error.
The two with casting also work for types originaly derived from Control as a
Panel and the others does not.

Cor
 
H

Herfried K. Wagner [MVP]

* Mattias Sjögren said:
5 less characters to type?! :) Since it produces the same code in
this context, I suppose it's a matter of style and personal preference
which one you choose.

IMO 'DirectCast' is better readable than 'CType' in this case.

;-)))
 
C

CJ Taylor

I know... I felt really dumb. =)

like really dumb..... this was meant to be humourous, but because its hard
to state inflection with text (hmmm........ thats an idea...) it appears to
have come out wrong.

I plutonically like all of you...

except for that dude that was commenting on scorpions article.. what was
with that?
 
C

Cor

In my opinion you can better not use them if you do not need them

directcast(ctr,textbox).text = "Herfried"

:)))))
IMO 'DirectCast' is better readable than 'CType' in this case.
Cor
 
C

CJ Taylor

In conclusion...

I aplogize.

apologize even...

CJ Taylor said:
I know... I felt really dumb. =)

like really dumb..... this was meant to be humourous, but because its hard
to state inflection with text (hmmm........ thats an idea...) it appears to
have come out wrong.

I plutonically like all of you...

except for that dude that was commenting on scorpions article.. what was
with that?
 
H

Herfried K. Wagner [MVP]

* "Cor said:
In my opinion you can better not use them if you do not need them

directcast(ctr,textbox).text = "Herfried"

:)))))

Depends on:

* Is 'Option Strict' set.
* 'ctr' is declared as 'Control'.

;-)
 
C

Cor

Hi Herfried,

Maybe I did not tell right what I did wanted to say
This goes fine also with option strict on

for each ctr as control in me.controls
ctr.text ="Herfried"
next

But a mass on your screen

:))
 

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