|
-
Aug 3rd, 2003, 02:16 PM
#1
Thread Starter
New Member
Working with form class collection <Solved!>
I am new to vb.net, previous just an asp guy.
I would like to create a startup form that displays a list box of all available forms and then by pressing a button, displays the form selected in the list box. This just helps the process of reviewing the UI of each form.
This is not as easy as it sounds because there is not a forms collection. I have a couple of questions/thoughts:
1) Forms are just classes in vb.net so there must be a way to loop through the classes and find ones whose names start with frm. Not elegant, but might work.
2) I have the following code behind a button that is related to a text box
---------------------------------------------------
Dim frmName As String
Dim T As Type
'Get form Name, put project name in front
frmName = "prj_New_Project." & Me.txbx_Form_Name.Text
T = GetType(frmName)
Dim F As Form = Activator.CreateInstance(T)
F.Show()
-------------------------------
There is a build error that says 'Type frmName is not defined'.
3) Does anyone know how to get this to work?
Thanks
Last edited by shrub; Aug 3rd, 2003 at 07:31 PM.
-
Aug 3rd, 2003, 03:42 PM
#2
Frenzied Member
If you need the name of forms in an assembly you can use System.Reflection.Assembly
VB Code:
Dim alltypes As Type()
Dim frmtype As Type
alltypes = System.Reflection.Assembly.GetExecutingAssembly.GetTypes
For Each frmtype In alltypes
If frmtype.IsSubclassOf(GetType(Form)) Then
Messagebox.Show(frmtype.FullName) ' FullName gives you the form name with its namespace
' , required to create an instnace, Name just gives you the type name.
End If
Next
Last edited by Lunatic3; Aug 3rd, 2003 at 03:59 PM.
'Heading for the automatic overload'
Marillion, Brave, The Great Escape, 1994
'How will WE stand the FIRE TOMORROW?'
Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979
-
Aug 3rd, 2003, 03:57 PM
#3
Frenzied Member
To open a form with its name you may use this code:
VB Code:
Dim frm As Form = Activator.CreateInstance(Type.GetType("Namespace.Formname"))
frm.Show
NameSpace.FormName is the Full name of form type, that you can get it from the previous example.
Last edited by Lunatic3; Aug 4th, 2003 at 12:23 AM.
'Heading for the automatic overload'
Marillion, Brave, The Great Escape, 1994
'How will WE stand the FIRE TOMORROW?'
Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979
-
Aug 3rd, 2003, 05:49 PM
#4
Thread Starter
New Member
Thanks!
That worked brilliantly! Thanks for your help.
For the record, here is what I did:
In the load event of the display form I put:
Dim alltypes As Type()
Dim frmtype As Type
alltypes = System.Reflection.Assembly.GetExecutingAssembly.GetTypes
For Each frmtype In alltypes
If frmtype.IsSubclassOf(GetType(Form)) Then
'MessageBox.Show(frmtype.FullName) ' FullName gives you the form name with its namespace (Use MessageBox for testing)
' Note: FullName is required to create an instance Name just gives you the type name.
' On the form is a listbox named ct_lst_Form_List, and adding the fullname of the form to that list.
ctl_lst_Form_List.Items.Add(frmtype.FullName)
End If
Next
On the form is a button with the following in the click event:
Dim frmFullName As String
frmFullName = Me.ctl_lst_Form_List.SelectedItem
Dim frm As Form = Activator.CreateInstance(Type.GetType(frmFullName))
frm.Show()
Select the form name in the list box and press the button, the form opens. Yipee!
Thanks again!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|