是否可以將您的form
轉換爲vb6中的自包含class module
?從表格製作班級模塊
2
A
回答
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
相關問題
- 1. 製作班級內的班級
- 2. 製作一個模板接受一個特定的班級/班級系列?
- 3. Java。從其他班級執行班級?
- 4. 從新班級取得班級
- 5. 從另一個班級訪問班級?
- 6. laravel查詢多個班級或表格
- 7. Python模塊VS班
- 8. 如何製作班級資產?
- 9. 如何從我的班級列表中找到從特定班級繼承的班級?
- 10. 製作表格單元格塊元素
- 11. 將日期格式從一個班級更改爲另一個班級
- 12. 在另一個班級製作幾個班級的對象數組
- 13. 從跨班級表中選擇兩個單元格
- 14. 無法使用VBA從班級名稱中刪除表格
- 15. 從列表中刪除班級
- 16. Eclipse CDT「新班級」模板
- 17. 從另一個班級撥打電話時限制班級的方法
- 18. 班級成員:模板化班級向量
- 19. 控制另一個班級的另一個班級
- 20. Angular2模塊級樣式表
- 21. :活動班級不工作
- 22. 添加/刪除班級工作,但點擊新班級不會
- 23. Bouml將批量操作添加到班級圖中的班級
- 24. TypeError(救援條款所要求的班級或模塊)
- 25. 紅寶石:如何在模塊中使用班級瓦爾斯
- 26. 從班級和調用方法拆分班級
- 27. 從另一個班級訪問「班級會話」
- 28. 將信息從一個班級拉到另一個班級
- 29. 如何從另一個班級訪問班級?
- 30. 一個班級如何從另一個班級請求信息?
問題是沒有意義的,因爲它代表... – 2010-01-12 02:13:02
你能編輯它以請更清楚一點? – 2010-01-12 02:21:52
您的意思是您希望能夠在ActiveX DLL或OCX(「自包含」)中包含表單,以便您可以在另一個項目中重新使用它?可能類似於「通用對話框」控件的工作方式? – 2010-01-12 02:26:44