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.
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.