|
-
Oct 16th, 2004, 07:20 PM
#1
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:
'Second form
Private Sub cmdClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdClose.Click
Me.Close()
Form2.Show() 'Doesnt work
End Sub
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Oct 16th, 2004, 07:51 PM
#2
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:
Private Sub cmdOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdOk.Click
Dim oReport As New Form2
Form1.ActiveForm.Hide()
oReport.ShowDialog()
End Sub
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Oct 16th, 2004, 08:15 PM
#3
Doh!
VB Code:
'Form1
Private Sub cmdOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdOk.Click
Dim oReport As New Form2
Me.Hide()
oReport.ShowDialog()
Me.Show()
End Sub
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Oct 17th, 2004, 12:20 AM
#4
OK, let's see if I write this right without taxes yelling at me...
VB Code:
'In module:
Public f1 As New Form1
Public f2 As Form2
Public Sub Main()
Application.Run(f1)
End Sub
'in form1 button click event
f2 = New Form2
Me.Hide()
f2.Show()
'in form2 close event
f1.Show()
HTH.
-
Oct 17th, 2004, 03:37 AM
#5
When I do it, I create a variable on the form you are showing like this:
So when you call the form, the code would be something like this:
VB Code:
Dim x As New frmX
x.myCaller = Me
Me.Hide()
x.Show()
And in the form that is shown, when I need to reshow the calling form, just use
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)
-
Oct 17th, 2004, 11:43 AM
#6
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Oct 18th, 2004, 01:26 AM
#7
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
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:
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:
Dim Form1 As New frmMain
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)
-
Oct 18th, 2004, 04:18 PM
#8
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|