|
-
Oct 8th, 2005, 05:29 PM
#1
Thread Starter
Frenzied Member
Closing form
Hey,
How do I close a form in the Load event?
I want not to load it under certain conditions.
Thanks,
Don't anthropomorphize computers -- they hate it
-
Oct 8th, 2005, 05:44 PM
#2
Frenzied Member
Re: Closing form
Your best bet would be a Sub Main
Example:
VB Code:
Module Module1
Public Sub Main()
Dim frm as new Form1
If something = True Then
Application.Run(frm)
Else
Something else
End If
End Sub
End Module
-
Oct 8th, 2005, 05:50 PM
#3
Re: Closing form
if you wanna use the form_load sub ...
VB Code:
[COLOR=Blue]Private Sub[/COLOR] Form1_Load([COLOR=Blue]ByVal[/COLOR] sender [COLOR=Blue]As[/COLOR] System.Object, [COLOR=Blue]ByVal[/COLOR] e [COLOR=Blue]As[/COLOR] System.EventArgs) [COLOR=Blue]Handles MyBase[/COLOR].Load
[COLOR=Blue]If[/COLOR] MessageBox.Show("open the application?", Application.ProductName, MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes [COLOR=Blue]Then[/COLOR]
[COLOR=Green]'/// do stuff because it's ok to load[/COLOR]
[COLOR=Blue]Else[/COLOR]
Close()
[COLOR=Blue]End If
End Sub[/COLOR]
Last edited by dynamic_sysop; Oct 8th, 2005 at 05:51 PM.
Reason: code tags playing silly beggers
~
if a post is resolved, please mark it as [Resolved]
protected string get_Signature(){return Censored;}
[vbcode][php] please use code tags when posting any code [/php][/vbcode]
-
Oct 8th, 2005, 06:45 PM
#4
Re: Closing form
I haven't tried to do this in .NET, but in VB6, closing a form during the load event caused erratic behavior. In .NET, I expect that it might be smoother, so you might just try calling Me.Close. If that still causes errors, you could set a form level boolean variable to true, then in the Activate event you can call Me.Close if the variable is true.
My usual boring signature: Nothing
 
-
Oct 9th, 2005, 11:34 AM
#5
Thread Starter
Frenzied Member
Re: Closing form
Yeah, I tried me.close in the load event and it doesn't do anthing. The load event just continues. I guess i'll use a public variable and check it from the calling routine.
Thanks,
Don't anthropomorphize computers -- they hate it
-
Oct 9th, 2005, 01:31 PM
#6
Re: Closing form
 Originally Posted by dynamic_sysop
if you wanna use the form_load sub ...
VB Code:
[COLOR=Blue]Private Sub[/COLOR] Form1_Load([COLOR=Blue]ByVal[/COLOR] sender [COLOR=Blue]As[/COLOR] System.Object, [COLOR=Blue]ByVal[/COLOR] e [COLOR=Blue]As[/COLOR] System.EventArgs) [COLOR=Blue]Handles MyBase[/COLOR].Load
[COLOR=Blue]If[/COLOR] MessageBox.Show("open the application?", Application.ProductName, MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes [COLOR=Blue]Then[/COLOR]
[COLOR=Green]'/// do stuff because it's ok to load[/COLOR]
[COLOR=Blue]Else[/COLOR]
Close()
[COLOR=Blue]End If
End Sub[/COLOR]
to expand on this, to avoid errors that may arise i would use the form's Activated event
but keep in mind that you'll need to use a boolean variable to know when the form's loading or not too
VB Code:
Private mblnFormLoading As Boolean = True
Private Sub Form1_Activated(...) Handles Mybase.Activated
If mblnFormLoading = True Then
mblnFormLoading = False
If MessageBox.Show("open the application?", Application.ProductName, MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.No Then Me.Close()
End If
End Sub
-
Oct 9th, 2005, 01:38 PM
#7
Re: Closing form
You dont have to create an instance of the form if your not going to show it.
VB Code:
Option Explicit On
Module modMain
Public goForm1 As Form1
Public Sub Main()
If SomeCondition = True Then
goForm1 = New Form1
Application.Run(goForm1)
Else
'Exit or whatever
End If
End Sub
End Module
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
|