Results 1 to 9 of 9

Thread: [02/03] Open Varible Named Form

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2005
    Posts
    35

    [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.

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,419

    Re: [02/03] Open Varible Named Form

    why do you need a form with a variable name?

  3. #3
    Lively Member
    Join Date
    May 2008
    Posts
    117

    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.
    whats the benefit of beeing rated?


  4. #4

    Thread Starter
    Member
    Join Date
    Nov 2005
    Posts
    35

    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

  5. #5

    Thread Starter
    Member
    Join Date
    Nov 2005
    Posts
    35

    Re: [02/03] Open Varible Named Form

    Nerd44, I tried the change you suggested an I got another system error. Thanks for trying.

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,419

    Re: [02/03] Open Varible Named Form

    use custom list items

    vb Code:
    1. Public Class listItem
    2.  
    3.     Public display As String
    4.     Public frm As Form
    5.  
    6.     Public Overrides Function ToString() As String
    7.         Return Me.display
    8.     End Function
    9.  
    10.     Public Sub New(ByVal display As String, ByVal form As Form)
    11.         Me.display = display
    12.         Me.frm = form
    13.     End Sub
    14.  
    15. End Class

    then to add to the list:

    vb Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    4.         Dim li As New listItem("two", Form2)
    5.         ListBox1.Items.Add(li)
    6.         li = New listItem("three", Form3)
    7.         ListBox1.Items.Add(li)
    8.     End Sub
    9.  
    10. end class

    to open the form when the list item is selected:

    vb Code:
    1. Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
    2.     DirectCast(ListBox1.SelectedItem, listItem).frm.Show()
    3. End Sub

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,419

    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:
    1. Private Sub Form2_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    2.     e.Cancel = True
    3.     Me.Hide()
    4. End Sub

  8. #8
    Junior Member Dr. Octagonapus's Avatar
    Join Date
    Aug 2008
    Location
    Hell, Pennsylvania
    Posts
    22

    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))

  9. #9
    Lively Member
    Join Date
    May 2008
    Posts
    117

    Re: [02/03] Open Varible Named Form

    I dont know what u've done, on my system it works:
    vbnet Code:
    1. Public Class Form1
    2.  
    3.    Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
    4.       Me.ListBox1.DisplayMember = "Name"
    5.       For Each F In New Type() {GetType(Form2), GetType(Form3)}
    6.          Me.ListBox1.Items.Add(F)
    7.       Next
    8.    End Sub
    9.  
    10.    Private Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ListBox1.SelectedIndexChanged
    11.       With DirectCast(sender, ListBox)
    12.          If .SelectedIndex < 0 Then Return
    13.          Dim O = Activator.CreateInstance(DirectCast(ListBox1.SelectedItem, Type))
    14.          DirectCast(O, Form).Show()
    15.       End With
    16.    End Sub
    17.  
    18. 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.
    whats the benefit of beeing rated?


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width