|
-
Feb 8th, 2002, 10:57 PM
#1
How can I give a usercontrol an Enable property
I'm working on an OCX and would like to give it an Enabled property. Short of putting in a lot of statements, I can not figure an easy way of doing it. I can't help but think there must be an API call that will accept the control as a prameter and allow you to toggle between Enabled and Disabled.
-
Feb 8th, 2002, 11:16 PM
#2
Stuck in the 80s
This is really simple, so I doubt I'm answering the right question.
The ActiveX Control Interface Wizard will produce this code for Enabling/Disabling a UserControl:
VB Code:
'WARNING! DO NOT REMOVE OR MODIFY THE FOLLOWING COMMENTED LINES!
'MappingInfo=UserControl,UserControl,-1,Enabled
Public Property Get Enabled() As Boolean
Enabled = UserControl.Enabled
End Property
Public Property Let Enabled(ByVal New_Enabled As Boolean)
UserControl.Enabled() = New_Enabled
PropertyChanged "Enabled"
End Property
'Load property values from storage
Private Sub UserControl_ReadProperties(PropBag As PropertyBag)
UserControl.Enabled = PropBag.ReadProperty("Enabled", True)
End Sub
'Write property values to storage
Private Sub UserControl_WriteProperties(PropBag As PropertyBag)
Call PropBag.WriteProperty("Enabled", UserControl.Enabled, True)
End Sub
-
Feb 8th, 2002, 11:31 PM
#3
That seems to do most of what I want. I assumed that since the usercontrol didn't have an Enabled property that you could not map something to it. I also found this API which pretty much does it also.
VB Code:
Private Declare Function EnableWindow Lib "user32" _
(ByVal hwnd As Long, ByVal fEnable As Long) As Long
If m_Enabled = False Then
EnableWindow UserControl.hwnd, 0
Else
EnableWindow UserControl.hwnd, 1
End If
-
Aug 24th, 2011, 04:53 AM
#4
Hyperactive Member
Re: How can I give a usercontrol an Enable property
Sorry for bringing this one back to life.
Using EnableWindow has it's problems, the one I encountered is one where focus isn't moved when the you disable the control from the outside (ofcourse I'm talking about a usercontrol which can have focus).
Mostly it works, but the following scenario will give you problems:
Create a form, and first put a textbox (tabindex 0), checkbox (tabindex 1), your control (tabindex 2), listbox (tabindex 3) and a commandbutton (tabindex 4) all default with Enabled set to true.
Code:
Option Explicit
Public Sub ShowMyForm()
Call Me.Show
Call pDisableControls
End Sub
Private Sub pDisableControls()
txt.Enabled = False
chk.Enabled = False
UserControl1.Enabled = False
lst.Enabled = False
End Sub
Private Sub chk_Click()
If chk.Value = vbChecked Then Call pDisableControls
End Sub
Private Sub cmd_Click()
Call pDisableControls
End Sub
Private Sub Form_Load()
' Call pDisableControls
End Sub
Create a module, and set your project to start with Sub Main
Code:
Sub Main()
Dim frm As Form1
Set frm = New Form1
Call frm.ShowMyForm
Set frm = Nothing
End Sub
If you run this code you'll see your usercontrol still has the focus (but is disabled) instead of the commandbutton.
Running pDisableControls in Form_Load or before me.Show or when you press the commandbutton will work perfectly. One solution is to set the Enabled property to false before the first vb6 control (or in the designer), but this still is annoying as it doesn't work as it's supposed to.
This problem is something I just saw recently, first I thought it might have something to do with Windows 7, but when i try the compiled version (VB6 SP6) on a XP machine it also has the same problem.
I can't remember the reason why I switched over from UserControl.Enable to EnableWindow as it was still during VB5 about 12 years ago (when the original component was written), and back then we didn't add details to the checkin into sourcesafe.. LOL. But I'm inclined to go back to using UserControl.Enabled, currently the change will only be in our internally used application.
If anyone has any solution to this problem I'm happy to hear it..
Edit: Just added the chk_Click example, which also shows the problem (and ofcourse is an example that is more common), to test this just comment out pDisableControls in ShowMyForm.
Last edited by SuperDre; Aug 24th, 2011 at 05:02 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|