如何在Visual Basic 6中在窗体内创建静态成员



在vb6中,我需要在表单中保持一些对象的静态(不变)。对象在窗体之前传递到窗体。显示vbModeless代码块。此表单中有一个列表,当项目单击事件触发时,对象将变为Nothing。

在我的应用程序中,有一个类和一个表单。我需要在表单中使用该类的一个对象。在主调用类中有一个子方法来加载表单并调用它的aainitialization方法。我将SelectStyleDlg对象传递给表单,如下所示。

下面我提到了我在调用类中使用的子方法。

Public Sub ShowTheDialog()  
With frmSelectStyle
.aaInitialize SelectStyleDlg:=SelectStyleDlg
.Show vbModeless
End With
End Sub

现在我要提到表格中的代码。

Option Explicit
Private mobjSelectStyleDlg As SelectStyleDlg
Public Static Sub aaInitialize(ByRef SelectStyleDlg As SelectStyleDlg)
Set mobjSelectStyleDlg  = SelectStyleDlg 
End Sub 
Private Sub lvwStyles_ItemClick(ByVal Item As MSComctlLib.ListItem)
If Not mobjSelectStyleDlg Is Nothing 
MsgBox  "Object is not nothing"
Else 
MsgBox  "Object is nothing"
End If
End Sub

当项目点击事件触发时,mobjSelectStyleDlg对象将变为空。请帮帮我。谢谢。

我不认为你的意思是静态的,我认为你只需要一个类实例变量。

在您的表格中:

Private mSelectStyleForm As frmSelectStyle
Private Sub cmdSelect_Click()
If mSelectStyleForm Is Nothing Then
Set mSelectStyleForm = New frmSelectStyle 
End If
Call mSelectStyleForm.aaInitialize(SelectStyleDlg:=SelectStyleDlg) 
Call mSelectStyleForm.Show(vbModeless) 'Or Modal
End Sub
Private Sub MyForm_Unload()
If Not mSelectStyleForm Is Nothing Then
'depending on if you unload the form earlier or not, do that too here
Set mSelectStyleForm = Nothing
End If
End Sub

最新更新