Event handling for dynamic controls in VB6

╄→гoц情女王★ 提交于 2019-12-18 17:32:25

问题


How can i achieve event handling for dynamic controls in VB6? Any ideas?


回答1:


The easiest way is to declare a module-level variable of the same type as the control, and use the WithEvents keyword. For example

Option Explicit
' Declare object variable as CommandButton and handle the events.'
Private WithEvents cmdObject As CommandButton 

Private Sub Form_Load()
   'Add button control and keep a reference in the WithEvents variable'
   Set cmdObject = Form1.Controls.Add("VB.CommandButton", "cmdOne")
   cmdObject.Visible = True
   cmdObject.Caption = "Dynamic CommandButton"
End Sub

'Handle the events of the dynamically-added control'
Private Sub cmdObject_Click()
    Print "This is a dynamically added control"
End Sub

There are more complicated variations if you want to be able to handle events from many different controls, perhaps of different types, through one central routine.




回答2:


That depends on whether the control is intrinsic or not.

This article explains it.



来源:https://stackoverflow.com/questions/2355697/event-handling-for-dynamic-controls-in-vb6

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!