473,406 Members | 2,620 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,406 software developers and data experts.

Traversing Controls

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
4 1189
"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
"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: w.p. | last post by:
Hello! I want change default tab traversing in my app. But i don't know how to do it :( Belowe i include simple example - i want change default tab order: radiobutton "mode11" -> radiobutton...
2
by: Jim Cobban | last post by:
I am using Xerces to read an XML file and load it into a DOM so I can update it and subsequently serialize the updated DOM. The problem I have is that as I traverse the DOM I would like to inform...
3
by: Plamen Valtchev | last post by:
This is my problem: From JavaScript I want to find the list of all defined/loaded JavaScript functions/objects/names within the current scope (html page in a browser). the page could contain...
0
by: elziko | last post by:
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...
4
by: plmanikandan | last post by:
Hi, I am new to link list programming.I need to traverse from the end of link list.Is there any way to find the end of link list without traversing from start(i.e traversing from first to find the...
0
by: df | last post by:
The user control can not be much simple: <%@ Control Language="C#" AutoEventWireup="true" CodeFile="SortOnListByClass.ascx.cs" Inherits="SortOnListByClass" %> <asp:HyperLink ID="SortOnName"...
30
by: asit | last post by:
We kno that data can be pushed onto the stack or popped 4m it. Can stack be traversed ??
1
by: somcool | last post by:
I am facing an error while traversing a query in MS Access Details - When I click a button, a form which has the query opens up. There are certain fields which are in the form of combo box in the...
4
by: sutejok | last post by:
Hi, I'm thinking of something like Ruby's Watir, where we can give it a command like - open a web address - click button labelled "submit" - select drop down list labelled "age", select 4th...
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: 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
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.