【问题标题】:Winforms - MDI Parent refresh/re-activateWinforms - MDI 父级刷新/重新激活
【发布时间】:2015-01-28 05:57:19
【问题描述】:

我有一个 MDI 父表单,其中包含应用程序的菜单条。我的应用程序启动文件是加载时调用子登录表单的 MDI 父表单。代码如下:

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

菜单条有一个注销标签,单击该标签会禁用菜单条并再次显示登录表单。 布尔字段 LoginSucceeded 确定用户凭据的成功验证,并根据授予该用户的访问权限设置菜单。我的问题是第一次根据用户的访问权限正确设置 MDI 父级上的主菜单。注销并再次登录后,我想再次相应地设置主菜单,但没有发生。

MDI Parent 上的 Form_Load 事件只执行一次。

第二次获得焦点时重新绘制 MDI 父级的任何提示。

谢谢, ZK

我的注销代码如下:

        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

【问题讨论】:

  • 你把这段代码放在哪里了? Form.Load 事件?

标签: vb.net winforms mdi


【解决方案1】:

将您的登录代码移动到主窗体中自己的方法中,以便您可以多次调用它:

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

您也不需要“LoginSucceeded”变量。您可以通过在登录表单中将 DialogResult 设置为 OK 将成功/失败传递回主表单:

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

【讨论】:

    【解决方案2】:

    这是对您代码的假设,我猜您在 Form.Load 事件中添加了代码。 Form.Load 事件仅在表单首次显示时引发。

    根据 MSDN Form.Load 事件

    在表单第一次显示之前发生。

    现在,当您注销时,您将表单的 visibility 设置为 false。所以我建议您将代码从Form.Load 事件移至Form.VisibleChanged 事件。

    根据 MSDN Form.Load 事件

    在 Visible 属性值更改时发生。

    【讨论】:

    • 您好,开发人员,感谢您的提示。我将代码段从 Load Event 移到了 VisibleChanged 事件中,但效果还是一样的。我只从登录表单传递 LoginSucceeded 布尔值。我第一次登录时它工作正常。在我注销然后再次尝试登录后,菜单(tabMainMenu)根本不显示。
    • @user4501566 编辑您的帖子并将其添加到那里,而不是在 cmets 中
    • 我什至试图让 tabMainMenu 从登录表单中可见,但它仍然没有执行第二次 MDI Parent 获得焦点
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-26
    • 2010-09-24
    • 1970-01-01
    • 1970-01-01
    • 2019-08-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多