Microsoft KB Archive/177996

= BUG: UserControl Event Is Not Raised from a Modal Form =

Article ID: 177996

Article Last Modified on 3/10/2005

-

APPLIES TO


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

-



This article was previously published under Q177996



SYMPTOMS
If an event is raised from a UserControl while a modal form is active, the event is not executed.



CAUSE
The event is not executed if the UserControl is compiled into an OCX and referenced from a project that is run in the IDE.



RESOLUTION
There are two workarounds to this problem:
 * 1) Use the compiled ActiveX component (OCX) only with compiled client programs (EXE). In other words, both Visual Basic projects are compiled.

-or-
 * 1) Use both the ActiveX component and the client project in the IDE in a project group. In other words, both Visual Basic projects are in design mode.



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



MORE INFORMATION
The following section shows you how to create a sample project that demonstrates this problem. To reproduce this behavior, you first need to create a compiled ActiveX control with a custom event. Then you make a reference to this control from another project and run that project in the IDE.

Create the ActiveX Control
 Start a new ActiveX control project in Visual Basic. UserControl1 is created by default. Add a new form to the project. Add a CommandButton to Form1.  Copy the following code to the Code window of UserControl1: Option Explicit Private WithEvents MyCommand As CommandButton Event TestEvent

Private Sub MyCommand_Click RaiseEvent TestEvent End Sub

Private Sub UserControl_Click Set MyCommand = Form1.Command1 Form1.Caption = "UserControl Form" Form1.Show vbModal End Sub  Compile UserControl1 into an OCX file. Close all open windows associated with UserControl1.

Create the Sample Project
 Start a new Standard EXE project in Visual Basic. Form1 is created by default.</li> Make a reference to the ActiveX control you created in the previous section by completing the following steps:

 From the Project menu, click Components. The Components dialog box is displayed.</li> Click Browse to open a dialog box that allows you to select the ActiveX component created in the previous section.</li> Click OK to close the dialog boxes.</li></ol> </li> Add the ActiveX control to the Form1 form. Adjust the ActiveX control size to take up most of the form.</li>  Copy the following code to the Code window of Form1: Option Explicit

Private Sub UserControl11_TestEvent MsgBox "In the User control test event" End Sub </li> Press the F5 key to run the project and click on Form1. The UserControl Form appears. Click Command1 and note that the message box indicating the custom event has executed does not appear.</li> Make the project into an EXE and run the EXE. Click Form1. The UserControl Form appears. Click Command1 and note that the message box indicating the custom event has executed does appear.</li></ol>

<div class="references_section">