0

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
1
  • Where did you place this code? Form.Load event? Jan 28, 2015 at 6:08

2 Answers 2

0

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
0
0

Here are presumptions on your code, I'm guessing you added the code about on the Form.Load event. The Form.Load event only gets raised when the form is shown for the first time.

According to MSDN Form.Load Event

Occurs before a form is displayed for the first time.

And now, when you Log-Off, you're setting the visibility of the form to false. So what I suggest is you move your code from the Form.Load event to the Form.VisibleChanged event.

According to MSDN Form.Load Event

Occurs when the Visible property value changes.

3
  • Hi Dev, Thanks for the tip. I moved the code segment from the Load Event to the VisibleChanged event but the effect is still the same. I am only passing the LoginSucceeded boolean value from the Login Form. It works ok the first time I login. After I logout and then try to login again, the menu (tabMainMenu) is not displayed at all. Jan 28, 2015 at 6:14
  • @user4501566 edit your post and add it there, not in the comments Jan 28, 2015 at 6:43
  • I even tried to make the tabMainMenu visible from the Login form but it still does not execute the 2nd time the MDI Parent receives focus Jan 28, 2015 at 7:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.