Results 1 to 7 of 7

Thread: Closing form

  1. #1

    Thread Starter
    Frenzied Member vbgladiator's Avatar
    Join Date
    May 2001
    Posts
    1,950

    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

  2. #2
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,521

    Re: Closing form

    Your best bet would be a Sub Main

    Example:
    VB Code:
    1. Module Module1
    2.    Public Sub Main()
    3.       Dim frm as new Form1
    4.       If something = True Then
    5.           Application.Run(frm)
    6.       Else
    7.          Something else
    8.       End If
    9.    End Sub
    10. End Module

  3. #3
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142

    Re: Closing form

    if you wanna use the form_load sub ...
    VB Code:
    1. [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
    2.         [COLOR=Blue]If[/COLOR] MessageBox.Show("open the application?", Application.ProductName, MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes [COLOR=Blue]Then[/COLOR]
    3.             [COLOR=Green]'/// do stuff because it's ok to load[/COLOR]
    4.         [COLOR=Blue]Else[/COLOR]
    5.             Close()
    6.         [COLOR=Blue]End If
    7.     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]

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    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

  5. #5

    Thread Starter
    Frenzied Member vbgladiator's Avatar
    Join Date
    May 2001
    Posts
    1,950

    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

  6. #6
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: Closing form

    Quote Originally Posted by dynamic_sysop
    if you wanna use the form_load sub ...
    VB Code:
    1. [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
    2.         [COLOR=Blue]If[/COLOR] MessageBox.Show("open the application?", Application.ProductName, MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes [COLOR=Blue]Then[/COLOR]
    3.             [COLOR=Green]'/// do stuff because it's ok to load[/COLOR]
    4.         [COLOR=Blue]Else[/COLOR]
    5.             Close()
    6.         [COLOR=Blue]End If
    7.     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:
    1. Private mblnFormLoading As Boolean = True
    2.  
    3. Private Sub Form1_Activated(...) Handles Mybase.Activated
    4.   If mblnFormLoading = True Then
    5.     mblnFormLoading = False
    6.     If MessageBox.Show("open the application?", Application.ProductName, MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.No Then Me.Close()
    7.   End If
    8. End Sub

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

    Re: Closing form

    You dont have to create an instance of the form if your not going to show it.
    VB Code:
    1. Option Explicit On
    2.  
    3. Module modMain
    4.  
    5.     Public goForm1 As Form1
    6.  
    7.     Public Sub Main()
    8.         If SomeCondition = True Then
    9.             goForm1 = New Form1
    10.             Application.Run(goForm1)
    11.         Else
    12.             'Exit or whatever
    13.         End If
    14.     End Sub
    15.  
    16. 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 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