Search code examples
vb.netwinformsmdi

Winforms - MDI Parent refresh/re-activate


I have a MDI Parent form which has a menustrip for the application. My application startup file is the MDI Parent form which on load calls a child login form. Code as below:

Dim myForm As Form = New Login
    Dim formResult As DialogResult = myForm.ShowDialog()
    If formResult = Windows.Forms.DialogResult.OK Then
        If LoginSucceeded = True Then
            Me.tabMainMenu.Visible = True
            ApplyUserAccess(eApp.DataAccess.DAL_UserSettings.SelectMenuSettingByUserID(glbUserID))
            myForm.Dispose()
        End If
    End If

The menustrip has a Logout label which when clicked disables the menu strip and displays the login form again. The boolean field LoginSucceeded determines a successful validation of the user credentials and sets the menu according to the access given to that user. My problem is the first time the main menu on the MDI parent is set properly based on the user's access. After logging out and logging in again, i wanted to set the main menu accordingly again which is not happening.

The Form_Load event on the MDI Parent is being executed only once.

Any tips of re-painting the MDI parent when it receives focus the 2nd time onwards.

Thanks, ZK

My code for the Logoff is as below:

        Dim blnLogout As DialogResult = MessageBox.Show("Are You Sure You Want To Logout?", "eApp", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
    If blnLogout = Windows.Forms.DialogResult.Yes Then
        SetToolbarMenuStyle()
        tabMainMenu.Visible = False
        LoginSucceeded = False
        blnShowLoginTab = True
        Dim myForm As Form = New Login
        myForm.MdiParent = Me
        myForm.WindowState = FormWindowState.Normal
        myForm.Show()
    End If

Solution

  • Move your login code to its own method in your main form so you can call it multiple times:

    Public Class Form1
    
        Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown          ValidateLogin()
            ValidateLogin()
        End Sub
    
        Private Sub LoginLogoutToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles LoginLogoutToolStripMenuItem.Click
            ValidateLogin()
        End Sub
    
        Private Sub ValidateLogin()
            ' disable appropriate main form elements so they can't access anything:
            Me.tabMainMenu.Visible = False
    
            Using myForm As New Login
                If myForm.ShowDialog(Me) = Windows.Forms.DialogResult.OK Then
                    ' login succeeded: re-enable main form elements                 
                    Me.tabMainMenu.Visible = True
                    ApplyUserAccess(eApp.DataAccess.DAL_UserSettings.SelectMenuSettingByUserID(glbUserID))
                Else
                    MessageBox.Show("Login Failed")
                End If
            End Using
        End Sub
    
    End Class
    

    You also don't need the "LoginSucceeded" variable. You can pass a success/failure back to the main form by setting DialogResult to OK in your Login form:

    Public Class Login
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            If True Then ' <-- perform your check
                Me.DialogResult = Windows.Forms.DialogResult.OK ' only return OK if login has succeeded
            End If
        End Sub
    
    End Class