Microsoft KB Archive/196131

= BUG: Dynamic Private UserControl Extender Events Do Not Show =

Article ID: 196131

Article Last Modified on 5/13/2003

-

APPLIES TO


 * Microsoft Visual Basic 6.0 Learning Edition
 * Microsoft Visual Basic 6.0 Professional Edition
 * Microsoft Visual Basic 6.0 Enterprise Edition

-



This article was previously published under Q196131



SYMPTOMS
If you declare the variable using early-binding syntax when you dynamically add an instance of a private UserControl, you cannot see or use any of the control's extender properties or events. You are only able to see and use the raw properties and events of the control (that is, those declared by the control itself).



CAUSE
Visual Basic 6.0 allows developers to dynamically add ActiveX controls to a form at run time. Typically, a VBControlExtender variable is used for the dynamically-added control, which performs late binding on the control. However, if the type of the control is known at compile time, you can declare the variable using early binding syntax like the following:

Dim WithEvents MyCtl As MyUserControl

However, if MyCtl is a private UserControl, Visual Basic binds directly to the interface for the control itself, bypassing the control extender it normally adds for ActiveX controls. As a result, your code will not see or be able to use any extender properties and events.



RESOLUTION
Declare the variable for your dynamically-added control as VBControlExtender, and Use the extender to access your control late-bound.



STATUS
Microsoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article.



Steps to Reproduce Behavior
 Create a new standard .exe project. Form1 is created by default. From the Project menu, add a new UserControl to the project.  Add the following code to the User Control: Event Click

Private Sub UserControl_Click RaiseEvent Click End Sub

Public Property Get Color As OLE_COLOR Color = UserControl.BackColor End Property

Public Property Let Color(Value As OLE_COLOR) UserControl.BackColor = Value End Property

 Close the User Control design window and open the code window for Form1.  Add the following code to the General Declarations section of Form1: Dim WithEvents MyCtl as UserControl1

</li>  Click on the Event list, and select MyCtl. You will only see the Click event. None of the extender events (for example, GotFocus, LostFocus, DragOver, and so forth) are available. Further, if you try to use any extender properties in code you will get an error. For Example: MyCtl.Visible = True 'This will cause a compile error.

</li>  To make the extender events available, add the following code to Form1: Dim WithEvents MyCtl As VBControlExtender

Private Sub Form_Load Set MyCtl = Controls.Add("Project1.UserControl1", "myctrl") MyCtl.Move 1, 1 MyCtl.Visible = True MyCtl.Color = &HFF0000 End Sub

Private Sub MyCtl_ObjectEvent(Info As EventInfo) MsgBox "This control event was fired: " & Info.Name End Sub

</li> Before you run the project, go to the Project menu and select Project1 Properties. Click the Make, and clear the "Remove information about unused ActiveX controls" check box.</li> Save and run the project.</li></ol>

RESULT: Click on the user control. You will see a message box indicating that the event fired properly.

<div class="references_section">