如何在Visual Basic 6中的表单内创建静态成员?

时间:2019-01-24 05:56:19

标签: vb6

我需要在vb 6的表单中保持一些对象静态(不更改)。该对象将传递给form.Show 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对象变为空。 请帮我。谢谢。

1 个答案:

答案 0 :(得分:0)

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

在您的表单内:

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