开发者

Making class module from a form

Is it poss开发者_如何学运维ible to convert your form to a self contained class module in vb6?


One simple way to do this is to create a new ActiveX DLL project in the VB6 IDE, and then add a new Form to the project. You also need a class, but you can just rename the default "Class1" that gets added to the project.

Create the form as you normally would, and then write a class that has a function to display the form, and optionally return information back to the caller (either via a return value, events, or public properties on the class). Once you compile the DLL, other projects can use your form by adding a reference to your DLL and instantiating the public class.

Below is a very simple example that demonstrates how you might create a generic login dialog that you can then re-use in multiple projects. The login dialog simply displays a login screen with username and password fields, and OK and Cancel buttons. A public class, LoginDialog, can be used by other projects to actually display the login form and to retrieve data from it (the actual username and password entered by the user, and whether or not the user cancelled the dialog). The public class is a wrapper around the functionality provided by the form.

Please note this is just a quick example to demonstrate the concept

  • Create a new Form and add it to the ActiveX DLL project. Rename it frmLogin and add the following controls to it:

    • A Textbox named txtUsername
    • A Textbox named txtPassword. Set the PasswordChar property to "*" (asterisk)
    • A CommandButton named cmdOK with the caption set to "OK"
    • A CommandButton named cmdCancel with the caption set to "Cancel"


  • Then add the following code to 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


  • Rename "Class1" in the project to "LoginDialog" and add the following code. This is the class other projects will use to diplay the login form (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
  • Compile the ActiveX project and give it a name (i.e. MyAppLoginUI) so you can easily identify it when you need to add it to other projects.

  • When you want to use the form in another project, go to Project -> References... in the menu, and add your ActiveX DLL to the project. You may have to click Browse... to find it. Below is an example of how other code might use the example login dialog we just created:


'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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜