Results 1 to 4 of 4

Thread: How can I give a usercontrol an Enable property

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    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
    VB Code:
    1. If Enabled = True
    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.

  2. #2
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    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:
    1. 'WARNING! DO NOT REMOVE OR MODIFY THE FOLLOWING COMMENTED LINES!
    2. 'MappingInfo=UserControl,UserControl,-1,Enabled
    3. Public Property Get Enabled() As Boolean
    4.   Enabled = UserControl.Enabled
    5. End Property
    6.  
    7. Public Property Let Enabled(ByVal New_Enabled As Boolean)
    8.   UserControl.Enabled() = New_Enabled
    9.   PropertyChanged "Enabled"
    10. End Property
    11.  
    12. 'Load property values from storage
    13. Private Sub UserControl_ReadProperties(PropBag As PropertyBag)
    14.   UserControl.Enabled = PropBag.ReadProperty("Enabled", True)
    15. End Sub
    16.  
    17. 'Write property values to storage
    18. Private Sub UserControl_WriteProperties(PropBag As PropertyBag)
    19.   Call PropBag.WriteProperty("Enabled", UserControl.Enabled, True)
    20. End Sub
    My evil laugh has a squeak in it.

    kristopherwilson.com

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141
    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:
    1. Private Declare Function EnableWindow Lib "user32" _
    2.             (ByVal hwnd As Long, ByVal fEnable As Long) As Long
    3.  
    4.     If m_Enabled = False Then
    5.         EnableWindow UserControl.hwnd, 0
    6.     Else
    7.         EnableWindow UserControl.hwnd, 1
    8.     End If

  4. #4
    Hyperactive Member
    Join Date
    Mar 2009
    Posts
    264

    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
  •  



Click Here to Expand Forum to Full Width