Results 1 to 8 of 8

Thread: Show Form Issue

  1. #1

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

    Resolved Show Form Issue

    I am officially getting into .NET and I am having a hard time making
    the transition from VB6. I am trying to show a second form from a
    first form upon a simple button click. No problem there, but when
    the second form closes I want the first form to come back. When
    the second form is being shown the first form is hidden. So how
    do I get it back?

    VB Code:
    1. 'Second form
    2.     Private Sub cmdClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdClose.Click
    3.         Me.Close()
    4.         Form2.Show() 'Doesnt work
    5.     End Sub
    6. End Class
    Thanks for any help.
    Last edited by RobDog888; Oct 16th, 2004 at 08:15 PM.
    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

  2. #2

    Thread Starter
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    I am doing this from the first form to bring up the second form, but
    I dont want the first form to be visible while the second form is
    open.


    VB Code:
    1. Private Sub cmdOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdOk.Click
    2.         Dim oReport As New Form2
    3.         Form1.ActiveForm.Hide()
    4.         oReport.ShowDialog()
    5.     End Sub
    6. End Class
    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

    Thread Starter
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    Doh!
    VB Code:
    1. 'Form1
    2.     Private Sub cmdOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdOk.Click
    3.         Dim oReport As New Form2
    4.         Me.Hide()
    5.         oReport.ShowDialog()
    6.         Me.Show()
    7.     End Sub
    8. End Class
    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

  4. #4
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    OK, let's see if I write this right without taxes yelling at me...

    VB Code:
    1. 'In module:
    2.  
    3.  
    4.     Public f1 As New Form1
    5.     Public f2 As Form2
    6.  
    7.     Public Sub Main()
    8.  
    9.         Application.Run(f1)
    10.  
    11.     End Sub
    12.  
    13.  
    14. 'in form1 button click event
    15.  
    16.  
    17.         f2 = New Form2
    18.         Me.Hide()
    19.         f2.Show()
    20.  
    21. 'in form2 close event
    22.  
    23.  f1.Show()

    HTH.

  5. #5
    Frenzied Member Ideas Man's Avatar
    Join Date
    Aug 2002
    Location
    Australia
    Posts
    1,718
    When I do it, I create a variable on the form you are showing like this:

    VB Code:
    1. Dim myCaller as Form

    So when you call the form, the code would be something like this:

    VB Code:
    1. Dim x As New frmX
    2.  
    3. x.myCaller = Me
    4. Me.Hide()
    5. x.Show()

    And in the form that is shown, when I need to reshow the calling form, just use
    VB Code:
    1. myCaller.Show

    Of course, you could also put this into the forms Sub New then you can't forget to set it.
    I use Microsoft Visual Basic 2005. (Therefore, most code samples I provide will be based around the .NET Framework v2.0, unless otherwise specified)

  6. #6

    Thread Starter
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    Why cant you just reference the form object in the form itself
    like you can do in vb6? I'm sure there is a good reason, but just
    wish it was the same as it was for 6.

    I understand the code posted but still why does it have to be
    that way?
    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

  7. #7
    Frenzied Member Ideas Man's Avatar
    Join Date
    Aug 2002
    Location
    Australia
    Posts
    1,718
    Because forms are classes and like any class, you need a reference to be able to modify the data stored in the class.

    Declaring a variable such as
    VB Code:
    1. Dim MainForm As frmMain
    Doesn't contain any data, rather is an object holder. If you were to try and use some of the methods that class contains, you will get an Object reference not set to an instance of an object. exception. So to give it a value that you can use, you can either pass a reference of an already created object to the holder, or create a new one such as:
    VB Code:
    1. Dim MainForm As New frmMain

    By doing this, you can then modify public properties and methods of the form such as showing it or retrieving a textbox value.

    One important note to remember,
    VB Code:
    1. Dim Form1 As New frmMain
    2. Dim Form2 As New frmMain
    Although they look the same, they are infact two different objects, so if you set the text for Form1, Form2 will not have the same text.

    To finish off, think of it as a blueprint, the same blueprint can make unlimited buildings, but you cannot put a computer in a plan of a room and use it, unless you physically place it into the room of the constructed building, and even then, if there are a number of buildings that are the same with the same type of computer, there will be different things on them at the same time.
    I use Microsoft Visual Basic 2005. (Therefore, most code samples I provide will be based around the .NET Framework v2.0, unless otherwise specified)

  8. #8

    Thread Starter
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    Thanks guys. I think I got it now. I also went through some
    tutorials on the M$ site.

    Thanks for the replies.
    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

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