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

时间:2012-02-03 04:41:42

标签: forms vb6

我们的第一个表格是LOG IN表格。我可以在登录后打开下一个表格吗?

3 个答案:

答案 0 :(得分:11)

在您的登录表单中,我假设您在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指的是您完成登录表单。

答案 1 :(得分:1)

您需要在表单上调用Show,该表格需要在登录表单后显示。您可以阅读有关Understanding Forms and form events

的更多信息

答案 2 :(得分:1)

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

而是让主窗体成为第一个,并在其Load事件中将您的登录窗体显示为模式对话框。这可以通过在其上显示主要表单来完成。示例基于标准模板“登录对话框”表单,其中包含一些代码更改:

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