Event handling for dynamic controls in VB6

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

+4
source share
2 answers

The easiest way is to declare a module level variable of the same type as the control and use the WithEvents keyword. for instance

 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 complex options if you want to be able to handle events from different controls, possibly of different types, through one central procedure.

+2
source

It depends on whether the control is internal or not.

This article explains this.

+2
source

Source: https://habr.com/ru/post/1302727/


All Articles