By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,700 Members | 1,473 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,700 IT Pros & Developers. It's quick & easy.

Traversing Controls

P: n/a
I have a class ("ClassB") that inherits from 'ClassA' which inherist
from UserControl.

I then put ClassB onto a form. Is it then possible to tracerse all the
controls on the form that inherit from ClassA.

I would expect the following code to work... bring up ClassB from the
collection.

For Each component as ClassA in Form1.Controls
'this should give any control that is based on ClassA
Next

However, I get the following exception:

"Specified cast is not valid"

Is this not possible? I could have several different classes, all
inheriting from ClassA. I dont know what these classes will be until
run-time so I need to traverse by base class.

TIA
Nov 21 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
"elziko" <el****@NOTSPAMMINGyahoo.co.uk> schrieb:
I have a class ("ClassB") that inherits from 'ClassA' which inherist
from UserControl.

I then put ClassB onto a form. Is it then possible to tracerse all the
controls on the form that inherit from ClassA.

I would expect the following code to work... bring up ClassB from the
collection.

For Each component as ClassA in Form1.Controls
'this should give any control that is based on ClassA
Next


\\\
For Each component As Control In Form1.Controls
If TypeOf component Is ClassA Then
DirectCast(component, ClassA).<...> = ...
End If
Next component
///

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 21 '05 #2

P: n/a
"elziko" <el****@NOTSPAMMINGyahoo.co.uk> schrieb:
I have a class ("ClassB") that inherits from 'ClassA' which inherist
from UserControl.

I then put ClassB onto a form. Is it then possible to tracerse all the
controls on the form that inherit from ClassA.

I would expect the following code to work... bring up ClassB from the
collection.

For Each component as ClassA in Form1.Controls
'this should give any control that is based on ClassA
Next


\\\
For Each component As Control In Form1.Controls
If TypeOf component Is ClassA Then
DirectCast(component, ClassA).<...> = ...
End If
Next component
///

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 21 '05 #3

P: n/a
On Fri, 12 Nov 2004 11:45:28 +0000, elziko wrote:
For Each component as ClassA in Form1.Controls
'this should give any control that is based on ClassA
Next


That will iterate through ALL the controls on Form1 regardless of what type
they are. If one of them happens NOT to be a ClassA, then the cast will
fail.

You could try something like the following, but there are also plenty of
examples on this newsgroup:

For Each component As Control In Form1.Controls
If component Is ClassA Then
'Do something with component here
End If
Next
--
Chris

dunawayc[AT]sbcglobal_lunchmeat_[DOT]net

To send me an E-mail, remove the "[", "]", underscores ,lunchmeat, and
replace certain words in my E-Mail address.
Nov 21 '05 #4

P: n/a
On Fri, 12 Nov 2004 11:45:28 +0000, elziko wrote:
For Each component as ClassA in Form1.Controls
'this should give any control that is based on ClassA
Next


That will iterate through ALL the controls on Form1 regardless of what type
they are. If one of them happens NOT to be a ClassA, then the cast will
fail.

You could try something like the following, but there are also plenty of
examples on this newsgroup:

For Each component As Control In Form1.Controls
If component Is ClassA Then
'Do something with component here
End If
Next
--
Chris

dunawayc[AT]sbcglobal_lunchmeat_[DOT]net

To send me an E-mail, remove the "[", "]", underscores ,lunchmeat, and
replace certain words in my E-Mail address.
Nov 21 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.