【问题标题】:VB.NET: Problem with Multiple FormsVB.NET:多个表单的问题
【发布时间】:2011-09-15 22:06:07
【问题描述】:

大家好!我在 VB.NET 中有一个问题。在为登录屏幕创建和测试我的 GUI 以显示工资单系统的主页时,出现了这个问题。

启动画面正常加载,并出现登录表单。我输入我的用户名和密码(例如用户名:Admin,密码:12345),登录成功。问题出在:当主菜单出现时,登录屏幕再次出现,此时,该屏幕应该已经关闭。我在使用 Show、Hide 和 Close 时遇到任何问题吗?

这是我的三种形式的代码。

A.启动画面

Public Class frmSplashScreen

Private Sub tmrSplashScreen_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrSplashScreen.Tick
    Me.Hide()
    frmLogin.Focus()
    frmLogin.Show()
End Sub

End Class

B.登录表单(用于系统访问)

Public Class frmLogin

Public userName As String
Public passWord As String

Private Sub cmdExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdExit.Click

    End

End Sub

Private Sub cmdSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSubmit.Click

    userName = txtUsername.Text
    passWord = txtPassword.Text

    If userName = "Admin" And passWord = "12345" Then
        MsgBox("Access Granted! Welcome to BYTE!", MsgBoxStyle.Information, "Byte EGC Payroll System")
        Me.Close()
        frmMainMenu.Show()
        frmMainMenu.Focus()
    Else
        MsgBox("Access Denied!", MsgBoxStyle.Critical, "Byte EGC Payroll System")
    End If

End Sub

结束类

最后:

C.主菜单。

Public Class frmMainMenu

Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click

    End

End Sub

Private Sub frmMainMenu_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    Me.WindowState = FormWindowState.Maximized

End Sub

Private Sub AboutToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AboutToolStripMenuItem.Click

    MsgBox("Byte" & vbCrLf & "By: JU-CHAN", vbInformation, "Byte Payroll System")

End Sub

结束类

非常感谢您的帮助。谢谢! :)

【问题讨论】:

  • 闪屏的目的是让人们无缘无故地等待,从而惹恼他们吗?另外,我假设这是伪造的代码,并且您实际上没有像生产系统中那样硬编码的密码。对吗?
  • 嗯...您可能是对的,但登录屏幕的 if-else 仅用于让我的教师检查我的整个系统进行评分。我完全了解登录屏幕在实际程序中的实际用途。但是,我还是会在这里考虑您的帖子,其中您说启动画面是邪恶的。我同意这一点。 :)

标签: vb.net forms


【解决方案1】:

我打赌你的问题是你启动画面中的计时器仍在触发 _tick 事件并再次打开登录页面。

也许在隐藏启动画面之前添加tmrSplashScreen.Stop()

或者更好。完全摆脱启动画面,它们是邪恶的。

【讨论】:

    【解决方案2】:

    计时器不断计时,导致启动屏幕重新加载,然后隐藏,再次显示登录屏幕。

    尝试将其放入 tmrSplashScreen 刻度处理程序中:

    tmrSplashScreen.Enabled = False
    

    【讨论】:

      【解决方案3】:

      您的初始屏幕不应控制登录表单何时显示。启动画面应该只在程序启动期间在程序实际启动之前在后台进行大量处理时使用。只是为了这样做而显示一个启动屏幕会惹恼最终用户。

      由于您上面的基本登录屏幕看起来不会占用大量资源,因此更好的选择是使用以下流程:

      显示登录屏幕
      如果密码成功则
      - 显示启动画面
      - 在后台加载应用程序
      - 加载应用程序后,显示主窗口并隐藏启动画面

      一些示例代码如下:

      Module modMain
          'In a module 
          Public frmSpl As frmSplash
          Public frmMain As frmMainMenu
      
          Public Sub Main(ByVal args() as String)
              dim frmLogin as New frmLogin
      
              'Assume frmLogin is a modal form
              frmLogin.Show
      
              'A public property set on the Login form
              If frmLogin.Passed = True Then Do
                  'Load and display the splash screen
                  frmSpl = New frmSplash
                  frmSpl.Cursor = Cursors.WaitCursor
                  frmSpl.Show()
                  Application.DoEvents()
      
                  'If there is any code needed to run before displaying the Main Form
                  'do it here 
      
                  frmMain = New frmMainMenu
      
                  'Begin running standard application loop for frmMainMenu
                  Application.Run(frmMain)
              End If
          End Sub
      End Module
      

      然后在 frmMainMenu 中,您将有类似以下的代码:

      Public Class frmMainMenu
      
          Private Sub frmMainMenu_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
      
              Try
                  Me.Cursor = Cursors.WaitCursor
                  Me.SuspendLayout
      
                  Me.WindowState = FormWindowState.Maximized
      
                  'Put any other loading code needed for this form here
              Catch (ex as Exception)
                  'Handle exceptions here
              Finally
                  'Hide the splash screen
                  frmSpl.Hide()
                  frmSpl.Dispose()
      
                  'Display the form
                  Me.ResumeLayout
                  Me.Cursor = Cursors.Default
                  Me.Show
              End Try
      
          End Sub
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-06-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-12-28
        • 2011-08-17
        相关资源
        最近更新 更多