[02/03] Open Varible Named Form
I am trying to open a form using varible names. I can not get it to work.
Dim wInvItemSelected As String
Dim wIndex As Integer
wIndex = lstInvInqSelect.SelectedIndex
Dim WFormName As String
WFormName = Trim(InvMenusForm(wIndex))
Dim objNewForm As Object = Activator.CreateInstance(Type.GetType(WFormName)) <==== Error Here
Dim frm As Form = DirectCast(objNewForm, Form)
frm.Show()
The error is shown on a message box "Microsoft Development Enviroment"
an unhandled exception of the 'System.argumentnullexception' occurred in mscorlib.dll
Additional Information: Value cannot be null
I can not find the error, can anyone help.
Thanks in advance.
Re: [02/03] Open Varible Named Form
why do you need a form with a variable name?
Re: [02/03] Open Varible Named Form
I suppose, the name of the type is not correct and full qualified.
if possible, put System.Type-Items into ur listbox and set listbox.DisplayMember to "Name".
then u can go like
Dim objNewForm As Object = Activator.CreateInstance(Directcast(lstInvInqSelect.SelectedItem, System.Type))
and there is not to worry about wrong spelling and stuff.
Re: [02/03] Open Varible Named Form
.Paul.
The list box has tasks that the user will choose. Each task has a corresponding form associated with it. If the user selects the first item in the list box, say "Inventory By Class", I would like to open a form "InvByClass.vb". The name of the form is located in an array that matches the list box entries.
If there is another way for me to open the form "InvByClass.vb" that I will not know until selected by the user, I will be glad to use it.
Thanks for your help
Re: [02/03] Open Varible Named Form
Nerd44, I tried the change you suggested an I got another system error. Thanks for trying.
Re: [02/03] Open Varible Named Form
use custom list items
vb Code:
Public Class listItem
Public display As String
Public frm As Form
Public Overrides Function ToString() As String
Return Me.display
End Function
Public Sub New(ByVal display As String, ByVal form As Form)
Me.display = display
Me.frm = form
End Sub
End Class
then to add to the list:
vb Code:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim li As New listItem("two", Form2)
ListBox1.Items.Add(li)
li = New listItem("three", Form3)
ListBox1.Items.Add(li)
End Sub
end class
to open the form when the list item is selected:
vb Code:
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
DirectCast(ListBox1.SelectedItem, listItem).frm.Show()
End Sub
Re: [02/03] Open Varible Named Form
to allow for opening + closing the forms more than once, you need to cancel the form from closing + hide it instead
vb Code:
Private Sub Form2_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
e.Cancel = True
Me.Hide()
End Sub
Re: [02/03] Open Varible Named Form
Quote:
Originally Posted by JesseH
Activator.CreateInstance(Type.GetType(WFormName)) <==== Error Here
You probably need to add your project's namespace to the front.
Activator.CreateInstance(Type.GetType("MyProject." & WFormName))
Re: [02/03] Open Varible Named Form
I dont know what u've done, on my system it works:
vbnet Code:
Public Class Form1
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
Me.ListBox1.DisplayMember = "Name"
For Each F In New Type() {GetType(Form2), GetType(Form3)}
Me.ListBox1.Items.Add(F)
Next
End Sub
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ListBox1.SelectedIndexChanged
With DirectCast(sender, ListBox)
If .SelectedIndex < 0 Then Return
Dim O = Activator.CreateInstance(DirectCast(ListBox1.SelectedItem, Type))
DirectCast(O, Form).Show()
End With
End Sub
End Class
but its not very useful for anything, i think.
On MDI-Variations i use a further developed kind of this technique to manage some mdi-Children.