Loop through certain controls

  • Thread starter Thread starter Kevin L
  • Start date Start date
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?
 
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
 
Dim objPanel As Object

For Each objPanel In Me.Controls
If objPanel.GetType Is GetType(Panel) Then
'do something here
End If
Next
 
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
 
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
 
* 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?
 
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
 
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
 
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
 
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
 
* 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.

;-)))
 
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?
 
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
 
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?
 
* "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'.

;-)
 
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

:-))
 
Back
Top