|
-
Oct 8th, 2002, 07:12 PM
#1
Thread Starter
Junior Member
Opening Forms At Run-Time
I have a number of forms in my project that are opened by selecting a menuitem, but I want to give the user the flexibility to decide what menuitem opens what form. In earlier versions of VB, I could refer to the forms collection to achieve this. But in VB.Net it seems that the name of the form must be known at design time e.g dim f as new frmMain. Does anyone know how I can supply frmMain at run-time.
Thanks
erimus
-
Oct 8th, 2002, 07:27 PM
#2
Hyperactive Member
To allow users to decide what menuitems open what form you can dynamically add the click handlers for each menuitem at runtime.
i.e. have a procedure for the opening of each form depending on a parameter and then add a handler for the menuitem:-
addhandler menuitem1.click, addressof Openform(1)
addhandler menuitem2.click, addressof Openform(2)
public sub Openform(number as short)
if number = 1 then
dim f as new form1
f.show
else
dim f as new form2
end if
end sub
-
Oct 8th, 2002, 09:11 PM
#3
Member
Is this closer to what you're looking for?
VB Code:
Public Function LoadForm(ByVal myFormName As String) As Form
Dim myType As Type
Dim myForm As Form
myType = System.Type.GetType(myFormName, True, True)
myForm = Activator.CreateInstance(myType)
Return myForm
End Function
Of course, you'll probably want some exception handling in there.
Not sure, but you may need to change:
VB Code:
myType = System.Type.GetType(myFormName, True, True)
to:
VB Code:
myType = System.Type.GetType("ProjectName." & myFormName, True, True)
(Obviously, replacing "ProjectName" with your project )
Matthew Draper
[email protected]
"Genius may have its limitations, but stupidity is not thus handicapped." - Elbert Hubbard
"I like long walks, especially when they are taken by people who annoy me." - Noel Coward
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
|