How to close main form i. e login form after login and it should not close application

Dani AI

Generated

Good question. Since this thread is about a desktop WinForms login, the cleanest way to close the login window without killing the app is to control lifetime yourself rather than keeping the login hidden (as / suggested) or relying on the startup form behavior. Hiding works, but it leaves a window and resources around you do not need. @Mitja Bonca’s approach of gating startup on a login result is on the right track; here is a slightly more robust pattern that also handles logout cleanly.

Use a custom ApplicationContext. Start from Sub Main, show the login modally, then launch the real main form. When the main form closes, the app ends. The login form is disposed, not hidden.

' Program.vb
Imports System.Windows.Forms

Module Program
    <STAThread>
    Sub Main()
        Application.EnableVisualStyles()
        Application.SetCompatibleTextRenderingDefault(False)
        Application.Run(New BootstrapContext())
    End Sub
End Module

' BootstrapContext.vb
Public Class BootstrapContext
    Inherits ApplicationContext

    Public Sub New()
        Using login As New LoginForm()
            If login.ShowDialog() = DialogResult.OK Then
                Dim main As New MainForm() ' pass data from login if needed
                AddHandler main.FormClosed, Sub(_, __) Me.ExitThread()
                Me.MainForm = main
                main.Show()
            Else
                Me.ExitThread() ' user cancelled or auth failed
            End If
        End Using
    End Sub
End Class

How to wire it up:

  • Set the project startup object to Sub Main.
  • Keep the login lightweight; dispose it after ShowDialog returns.
  • For a logout feature, raise an event from MainForm, handle it in BootstrapContext by closing MainForm and re-running the login dialog before deciding to exit or relaunch MainForm.

If you are actually on ASP.NET (tag suggests it), there is no concept of closing a form; you would redirect after authentication and manage session state instead.

Recommended Answers

All 7 Replies

If login is ur startup form u cannot close it else ur application will be closed...

set the form as hide or visible false before showing the other form....

and then show ur main form

Hi harsh01ajmera,

Go to the menubar and select -> project -> Call function -> change Shutdown mode into: When last form closes.

That means that you can close your login form without closing your application.

Check out this way by using DialogResult (and passing data between forms or classes):

'PROGRAM class    
Class Program

    ''' <summary>
    ''' The main entry point for the application.
    ''' </summary>
    <STAThread()>  _
    Private Shared Sub Main()
        Application.EnableVisualStyles
        Application.SetCompatibleTextRenderingDefault(false)
        Dim loginData As String = ""
        Dim l As Login = New Login
        If (l.ShowDialog = DialogResult.OK) Then
            'code returns form login (if you want you can get some data from there and pass it further:
            loginData = l.MYPROPERTY
        End If
        Application.Run(New Form1(loginData))
    End Sub
End Class
' LOGIN:
Public Class Login
    Inherits Form

    Public Property MYPROPERTY As String
    End Property

    Public Sub New()
        MyBase.New
        InitializeComponent
    End Sub

    Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs)
        'all ok..
        'and close login form (and pass data if you want)
        MYPROPERTY = "hi from login"
        Me.DialogResult = DialogResult.OK
    End Sub
End Class
'MAIN FORM:
Public Class Form1
    Inherits Form

    Public Sub New(ByVal loginData As String)
        MyBase.New
        InitializeComponent
        MessageBox.Show(loginData)
        'Do with the data what ever you want to...
    End Sub
End Class

Me.Hide()

Or try

yourFormName.Hide()

Ill go with Mitja Bonca.
Don't open your Home Screen on Login form's button click event.
Make the login form to return the login status and check,
do appropriate actions(whether to open the home screen or to give user an error message) in the Main function.

that's what Bonca's code illustrates :)

Startup form cant be closed, but it can be hidden.

This is what worked for me

Set login form as startupform, set project property to end application when last form closes and enter Form1.hide()
[assuming form1 is your login form's name] directly into second form in the Form2_load sub.
Done, nothing else required

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.