Results 1 to 7 of 7

Thread: How to open a Form??

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jul 2000
    Location
    Denmark
    Posts
    16

    How to open a Form??

    Very basic...

    I'm brand new to Visual Basic.NET and trying to figure it out (making the first "Hello World")

    How does I operate forms like in VB6:

    VB Code:
    1. Private Sub Command1_Click()
    2. Form2.Show
    3. End Sub

    VB Code:
    1. Private Sub Command2_Click()
    2. Form2.Hide
    3. End Sub
    Claus
    CLA

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: How to open a Form??

    You can show a form several ways. This is to show it modally.

    VB Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.     Dim oForm As New Form2
    3.     oForm2.ShowDialog
    4. End Sub
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3
    Member
    Join Date
    Jun 2005
    Posts
    48

    Re: How to open a Form??

    oform2.showdialog() will make it so the user must finish with that form before moving on.

    oform2.show() will just make it appear.

    - Josh

  4. #4
    Member Markmellow's Avatar
    Join Date
    Nov 2004
    Posts
    55

    Re: How to open a Form??

    The main difference is that you have to set a variable to the form, and then access the forms properties through that variable. The form can also be hidden in VB .NET, but I'm not sure if you want to hide it, or close it. Closing it will give you back resources. Hiding keeps the resources in use and the form still loaded in memory and accessible through code. If you're not going to use the form anytime soon or it's a one shot, best close it. If you're going to keep using it but don't want it visible all the time then hide would probably be better so you're not constantly opening and closing it.

    The above code could be written like this:

    VB Code:
    1. Dim myForm as new Form2
    2.  
    3. Private Sub Command1_Click()
    4. myForm.ShowDialog
    5. End Sub

    VB Code:
    1. Private Sub Command2_Click()
    2. myForm.close ' or myForm.hide
    3. End Sub

  5. #5
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: How to open a Form??

    Actually, the form is instanciated only once in your example so when it is .Closed it will no longer be able to be shown.
    You should have it like this to allow the button click to show the form over and over and yet keep resources low.

    VB Code:
    1. Dim myForm As Form2
    2.  
    3. Private Sub Command1_Click()
    4.     If myForm Is Nothing Then
    5.         myForm = New Form2
    6.     End If
    7.     myForm.ShowDialog
    8. End Sub
    9.  
    10. Private Sub Command2_Click()
    11.     myForm.Close  ' or myForm.hide
    12. End Sub
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Jul 2000
    Location
    Denmark
    Posts
    16

    Re: How to open a Form??

    Thanks...

    That helped me get started!!

    Claus
    CLA

  7. #7
    New Member
    Join Date
    May 2005
    Posts
    7

    Re: How to open a Form??

    Not wanting to jump in on someone else's thread, but this pertains to what I'm doing... I've got a TreeView which has 4 nested nodes, much like a Windows Explorer tree, except that it will never go more than 4 nodes deep.

    Each subcatagory opens a different form (a dialog box) inside the main (parent) form. Only 1 of these forms will ever be seen at any given time. Should I instanciate all 4 forms and then just show the correct one and repopulate it, or should I dispose of each one when it is not needed?

    In the On_Click event for the TreeView I have code like this:

    Dim frmComp, frmDept, frmLoc, frmMachine, frmAssess As Form

    frmComp = New frmCompany
    frmComp.MdiParent = Me
    frmDept = New frmDepartment
    frmDept.MdiParent = Me
    frmLoc = New frmLocation
    frmLoc.MdiParent = Me
    frmMachine = New DataForm2
    frmMachine.MdiParent = Me

    Then, I show the form like this:

    If TreeView1.SelectedNode.Tag = "company" Then

    frmComp.Show()
    ElseIf TreeView1.SelectedNode.Tag = "department" Then
    frmDept.Show()
    ElseIf TreeView1.SelectedNode.Tag = "location" Then
    frmLoc.Show()
    ElseIf TreeView1.SelectedNode.Tag = "WE" Then
    frmMachine.Show()
    x = "WE"
    End If
    If x = "WE" Then
    gstrPassedNode = TreeView1.SelectedNode.Tag
    Else
    gstrPassedNode = TreeView1.SelectedNode.Text
    End If


    Just wondering if I'm on the right track here. Thanks.

    john

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