从表单创建类模块

时间:2010-01-12 02:12:28

标签: forms vb6

是否可以将您的form转换为vb6中的自包含class module

1 个答案:

答案 0 :(得分:7)

执行此操作的一种简单方法是在VB6 IDE中创建新的ActiveX DLL项目,然后将新表单添加到项目中。您还需要一个类,但您可以重命名添加到项目中的默认“Class1”。

像往常一样创建表单,然后编写一个具有显示表单的函数的类,并可选择将信息返回给调用者(通过类的返回值,事件或公共属性)。编译DLL后,其他项目可以通过添加对DLL的引用并实例化公共类来使用您的表单。

下面是一个非常简单的示例,演示了如何创建一个通用登录对话框,然后可以在多个项目中重复使用。登录对话框只显示一个包含用户名和密码字段的登录屏幕,以及“确定”和“取消”按钮。其他项目可以使用公共类LoginDialog来实际显示登录表单并从中检索数据(用户输入的实际用户名和密码,以及用户是否取消了对话框)。公共类是表单提供的功能的包装。

请注意,这只是一个展示概念的简单示例

  • 创建一个新表单并将其添加到ActiveX DLL项目中。将其重命名为 frmLogin 并向其添加以下控件:

    • 名为 txtUsername
    • 的文本框
    • 名为 txtPassword 的文本框。将 PasswordChar 属性设置为“*”(星号)
    • 名为 cmdOK 的CommandButton,标题设置为“OK”
    • 名为 cmdCancel 的CommandButton,标题设置为“取消”


  • 然后将以下代码添加到 frmLogin.frm


'frmLogin.frm'

Public Cancelled As Boolean 'Set if the user clicks Cancel or closes the form'

Private Sub cmdCancel_Click()
   'User cancelled the dialog by clicking Cancel...'
   Me.Cancelled = True 
   Me.Hide
End Sub

Private Sub Form_QueryUnload(Cancel As Integer)
   'User cancelled the dialog by closing the window...'
   Me.Cancelled = True
   Me.Hide
End Sub

Private Sub cmdOK_Click()
   'Make sure the user filled in both fields.'
   If Trim(txtUsername.Text) = "" And _
      Trim(txtPassword.Text) = "" Then

      MsgBox "You must enter both a username and password."
      Exit Sub

   ElseIf Trim(txtUsername.Text) = "" Then
      MsgBox "You must enter a username."
      Exit Sub
   ElseIf Trim(txtPassword.Text) = "" Then
      MsgBox "You must enter a password."
      Exit Sub
   End If

   'User filled in the necessary data - we can hide the form now'
   Me.Cancelled = False
   Me.Hide

End Sub


  • 将项目中的“Class1”重命名为“LoginDialog”并添加以下代码。这是其他项目将用于显示登录表单(frmLogin)的类:


'LoginDialog.cls'

'A public class that allows other projects to display a login        '
'dialog and retrieve the user`s login information and whether or not '
'they cancelled the dialog.                                          '
'This code assumes you have a form in the same project named frmLogin'
'and that it contains 2 textboxes, txtUsername and txtPassword, and  '
'2 command buttons, cmdOK and cmdCancel.                             '
'                                                                    '

Public Username As String          'The username entered by the user'
Public Password As String          'The password entered by the user'
Public CancelledByUser As Boolean  'True if the user cancels or closes the form'

'Shows a new Login form with the specified defaults filled in, if provided.'
'                                                                          '
'                                                                          '
Public Function Show()

   'Create the login form and fill in the defaults'
   Dim frm As frmLogin
   Set frm = New frmLogin

   frm.txtUsername = Me.Username
   frm.txtPassword = Me.Password

   'Shows the form until it is hidden or closed'
   frm.Show vbModal

   If frm.Cancelled Then
      Me.CancelledByUser = True
   Else
      'Get the username and password from the form'
      Me.Username = frm.txtUsername
      Me.Password = frm.txtPassword
      Me.CancelledByUser = False
   End If

   'Unload the form'
   Unload frm

End Function
  • 编译ActiveX项目并为其命名(即 MyAppLoginUI ),以便在需要将其添加到其他项目时轻松识别它。

  • 如果要在其他项目中使用该表单,请转到项目 - >在菜单中引用... ,并将ActiveX DLL添加到项目中。您可能需要点击Browse...才能找到它。下面是其他代码如何使用我们刚创建的示例登录对话框的示例:


'LoginExample.bas'

' A simple example of how another project might use     '
' the generic login form created in the previous steps. '
' This example displays the login screen, then tries    '
' to authenticate the user against a database.          '
'                                                       '
Public Sub PerformLogin()

    Dim login As New LoginDialog

    'Pre-fill the username with the username of the last user who logged in.'
    login.Username = GetSetting("MyApp", "Settings", "LastUser")

    'Show the login screen. It will stay up until the user clicks OK or Cancel'
    login.Show()

    'If the user cancelled the login form, exit now...'
    If login.CancelledByUser Then
        Exit Sub
    End If

    'Pretend DAL is a data access layer module...'
    If Not DAL.LoginUser(login.Username, login.Password)
        MsgBox "Invalid username or password.", vbCritical+vbOKOnly, "Login Failure"
    End If

End Sub