473,385 Members | 1,863 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Loop through certain controls

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?
Nov 20 '05 #1
21 1224
Dim objPanel As Object

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

--
Greetz,
Jan
__________________________________
Read my weblog: http://weblogs.asp.net/jan
"Kevin L" <no_spam@not_real_email.com> schreef in bericht
news:uf**************@tk2msftngp13.phx.gbl...
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?

Nov 20 '05 #2
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

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 20 '05 #3
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

"Kevin L" <no_spam@not_real_email.com> wrote in message
news:uf**************@tk2msftngp13.phx.gbl...
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?

Nov 20 '05 #4
Wow! 3 different ways, each just as correct... that was cool. =)
"Kevin L" <no_spam@not_real_email.com> wrote in message
news:uf**************@tk2msftngp13.phx.gbl...
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?

Nov 20 '05 #5
>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 [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 20 '05 #6
* "Kevin L" <no_spam@not_real_email.com> scripsit:
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?


<http://www.mvps.org/dotnet/dotnet/samples/controls/downloads/EnumerateControls.zip>

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #7
* Mattias Sjögren <ma********************@mvps.org> scripsit:
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)


Why not use 'DirectCast' here?

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #8
Herfried,
objPanel = CType(ctl, Panel)


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

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 20 '05 #9
OK, OK, I forgot the TypeOf (Doh!)

BW

"Brian W" <brianw@gold_death_2_spam_rush.com> wrote in message
news:ON**************@TK2MSFTNGP10.phx.gbl...
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

"Kevin L" <no_spam@not_real_email.com> wrote in message
news:uf**************@tk2msftngp13.phx.gbl...
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?


Nov 20 '05 #10
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
Nov 20 '05 #11
I hate all of you.
"Cor" <no*@non.com> wrote in message
news:eF**************@TK2MSFTNGP12.phx.gbl...
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

Nov 20 '05 #12
Cor
Hi CJ,

I was trying to help you.

Cor
I hate all of you.

Nov 20 '05 #13
* Mattias Sjögren <ma********************@mvps.org> scripsit:
objPanel = CType(ctl, Panel)


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.


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

;-)))

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #14
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" <no*@non.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Hi CJ,

I was trying to help you.

Cor
I hate all of you.


Nov 20 '05 #15
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
Nov 20 '05 #16
In conclusion...

I aplogize.

apologize even...

"CJ Taylor" <no****@blowgoats.com> wrote in message
news:10*************@corp.supernews.com...
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" <no*@non.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Hi CJ,

I was trying to help you.

Cor
I hate all of you.



Nov 20 '05 #17
* "Cor" <no*@non.com> scripsit:
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'.

;-)

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #18
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

:-))

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

:-)))))


Depends on:

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

;-)

Nov 20 '05 #19
Cor
Hi CJ

Not needed, I forgot you did not had your first coffee

Cor
Nov 20 '05 #20
It's funny and true. =) Makes a big difference. =)
"Cor" <no*@non.com> wrote in message
news:ud**************@tk2msftngp13.phx.gbl...
Hi CJ

Not needed, I forgot you did not had your first coffee

Cor

Nov 20 '05 #21
* "Cor" <no*@non.com> scripsit:
Maybe I did not tell right what I did wanted to say
This goes fine also with option strict on


That's exactly how I understood it.

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #22

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: trenchmouth | last post by:
Does anybody know how I can loop through all the properties in a class I have created? Can I use Me to refer to the properties? I'm using VB6. Many thanks.
4
by: Ryan Ternier | last post by:
Thanks for the previous help guys! I got my list box issue working, but now i'm trying to loop through all the items in my page. I want to find each listbox, once I do i strip the ID down to...
4
by: sck10 | last post by:
I changed my aspx page to use a master page. The problem is that I can no longer loop through the controls on the content page. My question is how do you loop through the controls on the master...
2
by: Calvin KD | last post by:
Hi everyone, I'd like to interrogate a web form to check for values of the controls on the form. The following codes worked when I execute it in a User Control that has some web controls on it but...
3
by: Kezza | last post by:
Hi There.. I have dynamically created some textbox and checkbox controls by adding them to a panel. Now I would like to get the values out. I have created a for each loop that I can see the...
1
by: chuckdfoster | last post by:
I have 4 datagrids (dg1, dg2, dg3, and dg4) on a webpage. I know how to loop through the rows in each individual datagrid, but I need to loop through all 4. Is there a way to loop through the...
8
by: dominique | last post by:
Hi, Is it possible (in vb.net with WinForms) to loop throw controls inside a container (form or panel) sorting the controls on a property (.tabindex for example) ? My problem : on several...
0
by: Bryan | last post by:
Visual basic 2005, .net 2.0: I pass my form's collection of controls to a public sub in a module that loops through and checks for any ErrorProvider text. The sub returns all the ErrorProvider...
5
by: sck10 | last post by:
Hello, I am trying to add the following to a App_Code class. The error I am getting references "Page.Controls". I would like to call this from my content page which uses MasterPages I read...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.