2010-01-12 59 views
2

是否可以將您的form轉換爲vb6中的自包含class module從表格製作班級模塊

+0

問題是沒有意義的,因爲它代表... – 2010-01-12 02:13:02

+0

你能編輯它以請更清楚一點? – 2010-01-12 02:21:52

+1

您的意思是您希望能夠在ActiveX DLL或OCX(「自包含」)中包含表單,以便您可以在另一個項目中重新使用它?可能類似於「通用對話框」控件的工作方式? – 2010-01-12 02:26:44

回答

7

一個簡單的方法是在VB6 IDE中創建一個新的ActiveX DLL項目,然後向該項目添加一個新窗體。您還需要一個類,但是您可以重命名添加到項目中的默認「Class1」。

按照通常的方式創建表單,然後編寫一個具有顯示錶單功能的類,並可選地將信息返回給調用方(通過返回值,事件或類的公共屬性) 。一旦編譯DLL,其他項目就可以通過添加對DLL的引用並實例化公共類來使用表單。

下面是一個非常簡單的例子,演示瞭如何創建一個通用登錄對話框,然後您可以在多個項目中重新使用該對話框。登錄對話框簡單地顯示一個包含用戶名和密碼字段的登錄屏幕,以及確定和取消按鈕。其他項目可以使用公共類LoginDialog實際顯示登錄表單並從中檢索數據(用戶輸入的實際用戶名和密碼,以及用戶是否取消對話)。公共類是對錶單提供的功能的包裝。

請注意,這僅僅是一個簡單的例子來證明這個概念

  • 創建一個新的表格,並把它添加到ActiveX DLL項目。其重命名爲frmLogin並添加以下控件是:

    • 一個TextBox命名txtUsername
    • 一個TextBox命名txtPassword。設置PasswordChar屬性設置爲「*」(星號)
    • 一個命令按鈕命名爲按鈕cmdOK設置爲「OK」
    • 命令按鈕標題命名cmdCancel設置爲「取消」
    標題


  • 然後添加FOL降脂代碼frmLogin.frm:該項目爲 「一個LoginDialog」


'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」,並添加以下代碼。這是類其他項目將使用diplay登錄表單(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)等等您可以在需要將其添加到其他項目時輕鬆識別它。

  • 當您想要使用另一個項目中的窗體時,請在菜單中的Project - > References ...,並將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