如何从当前表单打开另一个表单?

发布于 2025-01-01 13:53:04 字数 34 浏览 0 评论 0原文

我们的第一个表单是登录表单。登录后如何打开下一个表单?

Our first form is the LOG IN form..how can I open to the next form after logging in?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

浅听莫相离 2025-01-08 13:53:04

在您的登录表单中,我假设您在按钮控件的 Click 事件方法内执行验证。因此,您将得到如下内容:

Private Sub btnLogin_Click()
    If ValidatePassword(txtPassword.Text) Then
        ' The password is correct, so show the main form and close the login form
        MainForm.Show
        Unload Me
    Else
        ' The password is incorrect, so leave this form on screen
        MsgBox "Invalid password entered!", vbError
        txtPassword.SetFocus
    End If
End Sub

此代码的两个有趣的功能是:

  1. Show 方法,您可以在要显示的表单对象上调用该方法。
    在这种情况下,它可能是您的主窗体 - 将 MainForm 替换为它所调用的任何名称。

  2. Unload 语句,用于关闭并销毁指定的表单。
    在本例中,Me 指的是登录表单,因为您已经完成了它。

In your log-in form, I assume you perform your validation inside of the Click event method for a button control. So you would have something like:

Private Sub btnLogin_Click()
    If ValidatePassword(txtPassword.Text) Then
        ' The password is correct, so show the main form and close the login form
        MainForm.Show
        Unload Me
    Else
        ' The password is incorrect, so leave this form on screen
        MsgBox "Invalid password entered!", vbError
        txtPassword.SetFocus
    End If
End Sub

The two interesting features of this code are:

  1. The Show method, which you call on the form object that you want to show.
    In this case, it will probably be your main form—replace MainForm with whatever it is called.

  2. The Unload statement, which closes and destroys the specified form.
    In this case, Me refers to the login form since you're finished with it.

青柠芒果 2025-01-08 13:53:04

您需要在登录表单后需要显示的表单上调用Show。您可以阅读有关了解表单和表单事件的更多信息

You will need call Show on the form which needs to be displayed post login form. You can read more about Understanding Forms and form events

π浅易 2025-01-08 13:53:04

我的方法是避免尝试将登录表单作为第一个表单打开。

相反,让主窗体位于第一个,并在其加载事件中将登录窗体显示为模式对话框。这可以通过首先在其上进行显示来显示主窗体来完成。基于标准模板“登录对话框”表单的示例,并进行了一些代码更改:

frmMain.frm

Option Explicit

Private Sub Form_Load()
    Dim Control As Control

    Show
    frmLogin.Show vbModal, Me
    With frmLogin
        txtSuccess.Text = CStr(.LoginSucceeded)
        If .LoginSucceeded Then
            'Proceed normally, perhaps after capturing
            'the User Name, etc.
            txtUserName.Text = .User
            txtPassword.Text = .Password
        Else
            'Do "Unload Me" or disable all controls
            'as shown here, etc.
            For Each Control In Controls
                On Error Resume Next
                Control.Enabled = False
                On Error GoTo 0
            Next
        End If
    End With
    Unload frmLogin
End Sub

frmLogin.frm

Option Explicit

Public LoginSucceeded As Boolean
Public User As String
Public Password As String

Private Sub cmdCancel_Click()
    LoginSucceeded = False
    Hide
End Sub

Private Sub cmdOK_Click()
    'Check for correct password, hard-coded here.
    If txtPassword.Text = "password" Then
        LoginSucceeded = True
        User = txtUserName.Text
        Password = txtPassword.Text
        Hide
    Else
        MsgBox "Invalid Password, try again!", , "Login"
        With txtPassword
            .SetFocus
            .SelStart = 0
            .SelLength = Len(.Text)
        End With
    End If
End Sub

My approach is to avoid trying to open a logon Form as the first Form.

Instead let the main Form be first, and in its Load event Show your logon Form as a modal dialog. This can be done revealing the main Form first by doing a Show on it. Example based on the standard template "Log in Dialog" Form with some code changes:

frmMain.frm

Option Explicit

Private Sub Form_Load()
    Dim Control As Control

    Show
    frmLogin.Show vbModal, Me
    With frmLogin
        txtSuccess.Text = CStr(.LoginSucceeded)
        If .LoginSucceeded Then
            'Proceed normally, perhaps after capturing
            'the User Name, etc.
            txtUserName.Text = .User
            txtPassword.Text = .Password
        Else
            'Do "Unload Me" or disable all controls
            'as shown here, etc.
            For Each Control In Controls
                On Error Resume Next
                Control.Enabled = False
                On Error GoTo 0
            Next
        End If
    End With
    Unload frmLogin
End Sub

frmLogin.frm

Option Explicit

Public LoginSucceeded As Boolean
Public User As String
Public Password As String

Private Sub cmdCancel_Click()
    LoginSucceeded = False
    Hide
End Sub

Private Sub cmdOK_Click()
    'Check for correct password, hard-coded here.
    If txtPassword.Text = "password" Then
        LoginSucceeded = True
        User = txtUserName.Text
        Password = txtPassword.Text
        Hide
    Else
        MsgBox "Invalid Password, try again!", , "Login"
        With txtPassword
            .SetFocus
            .SelStart = 0
            .SelLength = Len(.Text)
        End With
    End If
End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文