Microsoft KB Archive/171972: Difference between revisions
(importing KB archive) |
m (Text replacement - """ to """) |
||
Line 59: | Line 59: | ||
<ol> | <ol> | ||
<li><p>In the General Declaration section of the module, place the following declaration: (This must be declared as Private if it is included in a form module.)</p> | <li><p>In the General Declaration section of the module, place the following declaration: (This must be declared as Private if it is included in a form module.)</p> | ||
<pre class="codesample"> Private Declare Function EnableWindow Lib | <pre class="codesample"> Private Declare Function EnableWindow Lib "user32" Alias _ | ||
"EnableWindow" (ByVal hwnd As Long, ByVal fEnable As Long) As Long | |||
</pre></li> | </pre></li> | ||
<li><p>The hWnd property, like all UserControl properties, requires a public property method to expose the property as part of the object's interface. Since you will not be setting a value, the Property Get is all that is needed.</p> | <li><p>The hWnd property, like all UserControl properties, requires a public property method to expose the property as part of the object's interface. Since you will not be setting a value, the Property Get is all that is needed.</p> | ||
Line 72: | Line 72: | ||
<ol style="list-style-type: lower-alpha;"> | <ol style="list-style-type: lower-alpha;"> | ||
<li>From the Tools menu, select Procedure Attributes.</li> | <li>From the Tools menu, select Procedure Attributes.</li> | ||
<li>Select | <li>Select "hWnd" from the Name drop-down list.</li> | ||
<li>Click the Advanced button.</li> | <li>Click the Advanced button.</li> | ||
<li>From the Procedure ID drop-down list, choose | <li>From the Procedure ID drop-down list, choose "hWnd." Click OK.</li></ol> | ||
</li> | </li> | ||
<li><p>The EnableWindow function can now be called to enable or disable the UserControl. To disable the UserControl:</p> | <li><p>The EnableWindow function can now be called to enable or disable the UserControl. To disable the UserControl:</p> | ||
Line 101: | Line 101: | ||
<ol> | <ol> | ||
<li>Create a new | <li>Create a new "ActiveX Control" project.</li> | ||
<li><p>Add the following code to the UserControl:</p> | <li><p>Add the following code to the UserControl:</p> | ||
<pre class="codesample"> Public Property Get enabled() As Boolean | <pre class="codesample"> Public Property Get enabled() As Boolean | ||
Line 112: | Line 112: | ||
Private Sub UserControl_Click() | Private Sub UserControl_Click() | ||
MsgBox | MsgBox "Control is not Disabled." | ||
End Sub | End Sub | ||
</pre></li> | </pre></li> | ||
<li>From the Tools menu, select Procedure Attributes.</li> | <li>From the Tools menu, select Procedure Attributes.</li> | ||
<li>Click the Advanced button.</li> | <li>Click the Advanced button.</li> | ||
<li>From the Procedure ID drop-down, chose | <li>From the Procedure ID drop-down, chose "Enabled." Click OK.</li> | ||
<li>Add a | <li>Add a "Standard EXE" project.</li> | ||
<li>Close the UserControl window, and add the UserControl to Form1.</li> | <li>Close the UserControl window, and add the UserControl to Form1.</li> | ||
<li><p>Add two CommandButtons to Form1. Set the following properties.</p> | <li><p>Add two CommandButtons to Form1. Set the following properties.</p> | ||
<pre class="fixed_text"> Name: Command1 Caption: | <pre class="fixed_text"> Name: Command1 Caption: "Enable Control" | ||
Name: Command2 Caption: | Name: Command2 Caption: "Disable Control" | ||
</pre></li> | </pre></li> | ||
<li><p>Add the following code to Form1.</p> | <li><p>Add the following code to Form1.</p> | ||
Line 140: | Line 140: | ||
</pre></li> | </pre></li> | ||
<li>Run the project.</li> | <li>Run the project.</li> | ||
<li>Note that clicking on the button labeled | <li>Note that clicking on the button labeled "Disable Control" does not prevent the message box from appearing when the UserControl is clicked.</li> | ||
<li>Stop the project and open Module1.</li> | <li>Stop the project and open Module1.</li> | ||
<li><p>In Sub Main(), change:</p> | <li><p>In Sub Main(), change:</p> | ||
Line 148: | Line 148: | ||
<pre class="codesample"> Form1.Show | <pre class="codesample"> Form1.Show | ||
</pre></li> | </pre></li> | ||
<li>Run the project again, and note that the | <li>Run the project again, and note that the "Disable Control" button now functions as expected and the message box does not appear.</li></ol> | ||
Latest revision as of 11:05, 21 July 2020
Article ID: 171972
Article Last Modified on 12/10/2003
APPLIES TO
- Microsoft Visual Basic 5.0 Control Creation Edition
- Microsoft Visual Basic 5.0 Learning Edition
- Microsoft Visual Basic 5.0 Professional Edition
- Microsoft Visual Basic 5.0 Enterprise Edition
This article was previously published under Q171972
SYMPTOMS
A UserControl created in Visual Basic and placed on a form that is opened modally does not respond to attempts made to disable it. Setting the Enabled property of the control behaves as expected on a non-modal form, but setting Enabled = False on a modal form has no effect.
RESOLUTION
You can use the Windows API function EnableWindow to enable/disable the ActiveX control.
In the General Declaration section of the module, place the following declaration: (This must be declared as Private if it is included in a form module.)
Private Declare Function EnableWindow Lib "user32" Alias _ "EnableWindow" (ByVal hwnd As Long, ByVal fEnable As Long) As Long
The hWnd property, like all UserControl properties, requires a public property method to expose the property as part of the object's interface. Since you will not be setting a value, the Property Get is all that is needed.
Public Property Get hWnd() as Long hWnd=UserControl.hWnd End Property
- Next, you need to associate this method with the hWnd property of the control:
- From the Tools menu, select Procedure Attributes.
- Select "hWnd" from the Name drop-down list.
- Click the Advanced button.
- From the Procedure ID drop-down list, choose "hWnd." Click OK.
The EnableWindow function can now be called to enable or disable the UserControl. To disable the UserControl:
Dim result as Long result = EnableWindow(Me.UserControl1.hWnd, 0)
To enable the UserControl:
Dim result as Long result = EnableWindow(Me.UserControl1.hWnd, 1)
STATUS
Microsoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article. This bug has been fixed in Visual Basic 6.0.
MORE INFORMATION
Steps to Reproduce Behavior
- Create a new "ActiveX Control" project.
Add the following code to the UserControl:
Public Property Get enabled() As Boolean enabled = UserControl.enabled End Property Public Property Let enabled(vNewValue As Boolean) UserControl.enabled = vNewValue End Property Private Sub UserControl_Click() MsgBox "Control is not Disabled." End Sub
- From the Tools menu, select Procedure Attributes.
- Click the Advanced button.
- From the Procedure ID drop-down, chose "Enabled." Click OK.
- Add a "Standard EXE" project.
- Close the UserControl window, and add the UserControl to Form1.
Add two CommandButtons to Form1. Set the following properties.
Name: Command1 Caption: "Enable Control" Name: Command2 Caption: "Disable Control"
Add the following code to Form1.
Private Sub Command1_Click() Me.UserControl11.Enabled = True End Sub Private Sub Command2_Click() Me.UserControl11.Enabled = False End Sub
- Add a standard module to the project.
Add the following code to Module1:
Public Sub Main() Form1.Show vbModal End Sub
- Run the project.
- Note that clicking on the button labeled "Disable Control" does not prevent the message box from appearing when the UserControl is clicked.
- Stop the project and open Module1.
In Sub Main(), change:
Form1.Show vbModal
to:
Form1.Show
- Run the project again, and note that the "Disable Control" button now functions as expected and the message box does not appear.
Additional query words: kbComp kbCtrl kbVBp kbdsd kbVBp500bug kbVBp600fix kbDSupport kbCtrlCreate kbAPI
Keywords: kbbug kbfix KB171972