How to enumerate all the forms

B

brambilla

Hi to all

I use windows CE embedded 6.0 R2 and i develop in VB.NET 2005. My
device is an x86. In my application i need to enumerate all the forms
(not only the forms opened).
My.Forms is not a collection and i cant use it into for...each
instruction...


Thanks in advance, any suggestions/helps will be highly appreciated
and excuse me for my bad english!
Ale
 
P

Paul G. Tobey [eMVP]

Clear that up a bit. You want to enumerate all of the form *types* that are
in loaded assemblies? That is, distinguish for us whether you're talking
about objects (actual instances), or classes. It would probably be a good
idea to tell us *what* you're trying to do, also, not just how you've
decided to do it.

Paul T.
 
B

brambilla

Clear that up a bit.  You want to enumerate all of the form *types* thatare
in loaded assemblies?  That is, distinguish for us whether you're talking
about objects (actual instances), or classes.  It would probably be a good
idea to tell us *what* you're trying to do, also, not just how you've
decided to do it.

Paul T.








- Show quoted text -

Well. I need to set the Tag property in all controls and forms (Tag=1
--> the control/form is enabled for the current user, Tag=0 --> the
control/form is disabled for the current user).
For the controls into forms i can set the Tag with the For...Each
instruction:
for ctrl as control in frmMain.controls then...
but for the forms ? I'm talking about classes (but i think is not very
important because my application show all the forms at startup and do
not never close them, therefore we can talking about objects).

I hope now is clear, thanks!
Ale
 
N

Neil Cowburn

In my application i need to enumerate all the forms
(not only the forms opened).

Use the IsSubclassOf method on each Type in the assembly to determine
if it is a Form.
 
P

Paul G. Tobey [eMVP]

You can't do it for classes. A class is a description of an object. A
class doesn't have a property that you can change the value of; only an
instantiated object has any run-time data (ignoring reflection data for
now).

So, to summarize, you want a way of noting which form types a given user is
allowed to create and which ones he is not allowed to create, yes? It seems
to me that a list of Type that includes those form types that the user can
create, against which each new attempt to create a form is checked makes
more sense. This does not involve enumerating all forms, by the way.

Paul T.

Clear that up a bit. You want to enumerate all of the form *types* that
are
in loaded assemblies? That is, distinguish for us whether you're talking
about objects (actual instances), or classes. It would probably be a good
idea to tell us *what* you're trying to do, also, not just how you've
decided to do it.

Paul T.








- Show quoted text -

Well. I need to set the Tag property in all controls and forms (Tag=1
--> the control/form is enabled for the current user, Tag=0 --> the
control/form is disabled for the current user).
For the controls into forms i can set the Tag with the For...Each
instruction:
for ctrl as control in frmMain.controls then...
but for the forms ? I'm talking about classes (but i think is not very
important because my application show all the forms at startup and do
not never close them, therefore we can talking about objects).

I hope now is clear, thanks!
Ale
 
B

brambilla

You can't do it for classes.  A class is a description of an object.  A
class doesn't have a property that you can change the value of; only an
instantiated object has any run-time data (ignoring reflection data for
now).

So, to summarize, you want a way of noting which form types a given user is
allowed to create and which ones he is not allowed to create, yes?  It seems
to me that a list of Type that includes those form types that the user can
create, against which each new attempt to create a form is checked makes
more sense.  This does not involve enumerating all forms, by the way.

Paul T.

Hi Paul
i try to reformulate:
exist a collection of forms ?
Thanks
Ale
 
B

brambilla

Use the IsSubclassOf method on each Type in the assembly to determine
if it is a Form.

Hi Neil
can you make an example in VB ?
I try with not achievement...
Thanks!
Ale
 
P

Paul G. Tobey [eMVP]

You mean objects that are already created which are of Form type? Not as
far as I know. However, it wouldn't be any trouble to create a class that
keeps a list of open forms and adjust the forms so that each one
automatically registers with this list on creation and deregisters on
destruction. I think you're going about this the wrong way, though, if
you've properly explained *what* you're trying to do, rather than how.
Don't get too stuck on your own implementation scheme; it's not necessarily
the best way.

Paul T.

You can't do it for classes. A class is a description of an object. A
class doesn't have a property that you can change the value of; only an
instantiated object has any run-time data (ignoring reflection data for
now).

So, to summarize, you want a way of noting which form types a given user
is
allowed to create and which ones he is not allowed to create, yes? It
seems
to me that a list of Type that includes those form types that the user can
create, against which each new attempt to create a form is checked makes
more sense. This does not involve enumerating all forms, by the way.

Paul T.

Hi Paul
i try to reformulate:
exist a collection of forms ?
Thanks
Ale
 
C

Chris Tacke, MVP

Show us what you've tried.

-Chris


Use the IsSubclassOf method on each Type in the assembly to determine
if it is a Form.

Hi Neil
can you make an example in VB ?
I try with not achievement...
Thanks!
Ale
 
N

Neil Cowburn

can you make an example in VB ?
I try with not achievement...

Well, you didn't post any code for us to dissect, so I'm really under
no obligation to do the same. However, I'm a helpful guy. This is
untested & not even checked if it will compile, but the general
principle is demonstrated:

' Define the base type
Dim baseType As Type = GetType(System.Windows.Forms.Form)


' Get all types in the current assembly
Dim types() As Type = Assembly.GetExecutingAssembly.GetTypes

' Enumerate the type array looking
' for classes that derive from Form
Dim i As Integer
For i = 0 To types.Length -1
Dim T As Type = types(i)

If T.IsSubclassOf(baseType) Then
' The current type, T, is a Form, so what
' you do with it is up to you
End If
Next

Next time, do NOT ask for help without posting your code. We're not
here to write your application for you.
 
B

brambilla

Hi Paul
i try to reformulate:
exist a collection of forms ?
Thanks
Ale

You mean objects that are already created which are of Form type?  Not
as
far as I know.  However, it wouldn't be any trouble to create a class
that
keeps a list of open forms and adjust the forms so that each one
automatically registers with this list on creation and deregisters on
destruction.  I think you're going about this the wrong way, though,
if
you've properly explained *what* you're trying to do, rather than how.
Don't get too stuck on your own implementation scheme; it's not
necessarily
the best way.
Paul T.


Thankyou very much for yours helps and suggestions (sometimes is very
hard to me explain in english and i dont know the object oriented
paradigm ...).
Ale
 
B

brambilla

can you make an example in VB ?
I try with not achievement...


Well, you didn't post any code for us to dissect, so I'm really under
no obligation to do the same. However, I'm a helpful guy. This is
untested & not even checked if it will compile, but the general
principle is demonstrated:
' Define the base type
Dim baseType As Type = GetType(System.Windows.Forms.Form)
' Get all types in the current assembly
Dim types() As Type = Assembly.GetExecutingAssembly.GetTypes
' Enumerate the type array looking
' for classes that derive from Form
Dim i As Integer
For i = 0 To types.Length -1
    Dim T As Type = types(i)
    If T.IsSubclassOf(baseType) Then
      ' The current type, T, is a Form, so what
      ' you do with it is up to you
    End If
Next
Next time, do NOT ask for help without posting your code. We're not
here to write your application for you.
Neil Cowburn MVP


Thankyou very much for yours helps and code (i'm sorry for my code but
it was just an experiment because i dont know the object oriented
paradigm ...).
Ale
 
P

Paul G. Tobey [eMVP]

I'm saying, "Do it yourself", adding each for, as it is created, by
modifying the constructor, to register with the list of forms. You can, as
Neil pointed out, enumerate all objects of type Form. That doesn't require
any modification of your form code, but will, of course, be slower than just
enumerating through a list that you already know just contains the forms you
care about.

Paul T.

Hi Paul
i try to reformulate:
exist a collection of forms ?
Thanks
Ale

You mean objects that are already created which are of Form type? Not
as
far as I know. However, it wouldn't be any trouble to create a class
that
keeps a list of open forms and adjust the forms so that each one
automatically registers with this list on creation and deregisters on
destruction. I think you're going about this the wrong way, though,
if
you've properly explained *what* you're trying to do, rather than how.
Don't get too stuck on your own implementation scheme; it's not
necessarily
the best way.
Paul T.


Thankyou very much for yours helps and suggestions (sometimes is very
hard to me explain in english and i dont know the object oriented
paradigm ...).
Ale
 

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