我在 VB6 中创建了一个简单的表单,并添加了一个简单的空白用户控件。唯一的代码是所有重大事件中的 Debug.Print 语句。
由于某种原因,当表单关闭时,用户控件中的事件顺序是:
Terminate
Initialize
Read Properties
Resize
为什么会发生这种情况?为什么在 Terminate 之后调用 Initialize、ReadProperties 和 Resize?我在 Microsoft 文档中找不到任何这方面的证据。
编辑:
这是代码。
用户控制:
VERSION 5.00
Begin VB.UserControl UserControl1
ClientHeight = 3600
ClientLeft = 0
ClientTop = 0
ClientWidth = 4800
ScaleHeight = 3600
ScaleWidth = 4800
End
Attribute VB_Name = "UserControl1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Private Sub UserControl_Initialize()
Debug.Print "Initialize"
End Sub
Private Sub UserControl_InitProperties()
Debug.Print "InitProperties"
End Sub
Private Sub UserControl_ReadProperties(PropBag As PropertyBag)
Debug.Print "ReadProperties"
End Sub
Private Sub UserControl_Resize()
Debug.Print "Resize"
End Sub
Private Sub UserControl_Show()
Debug.Print "Show"
End Sub
Private Sub UserControl_Terminate()
Debug.Print "Terminate"
End Sub
Private Sub UserControl_WriteProperties(PropBag As PropertyBag)
Debug.Print "WriteProperties"
End Sub
表格:
VERSION 5.00
Begin VB.Form Form1
Caption = "Form1"
ClientHeight = 3030
ClientLeft = 120
ClientTop = 450
ClientWidth = 4560
LinkTopic = "Form1"
ScaleHeight = 3030
ScaleWidth = 4560
StartUpPosition = 3 'Windows Default
Begin Project1.UserControl1 UserControl1
Height = 2535
Left = 240
TabIndex = 0
Top = 240
Width = 4095
_extentx = 7223
_extenty = 4471
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
最佳答案
Debug.Print
表示您在 IDE 中以 Debug模式运行它。
调试时,您会在表单卸载时看到Terminate
,然后您会在表单设计器的表单中重新加载用户控件时看到用户控件的重新初始化用于编辑。
编译后的 EXE 不会发生这种情况,您可以将 Debug.Print
替换为 MsgBox
来验证这一点。
关于VB6 在用户控件的终止事件之后调用初始化事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30063064/