Click here to Skip to main content
16,016,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I create a control that inherits from System.Windows.Forms.UserControl. Let’s say that my control contains a label and a textbox. I use my control in a form, inside a group box. When I disable the group box, both the control’s label and text box became disabled. I’m trying to have the label remain enabled, but I can’t.
I tried:
1. Overriding the WndProc method, capturing the WM_ENABLE message and disabling the text box at this point (if LPARAM is true), eventually enabled the label
2. Overriding the Sub OnEnabledChanged and, after calling Mybase. OnEnabledChanged, setting the label.Enabled to true
3. Creating a Public Overloads Property Enabled(), and setting the TextBox.Enabled property to the right value there, leaving the Label enabled
4. Handling the Me.EnabledChanged or the Mybase.EnabledChanged
5. Adding a panel to my control and including the label and the textbox. Then
5.1. Handle the OnEnabledChange of the panel, enabling the label in this method.
5.2. Enabling the panel in any of the four previous places where I tried to enable the label before (not yet #1), with the idea that enabling my container would let me manage the individual controls.

I always thought that this idea must be easy to accomplish, but it doesn’t seem so. Can anybody help? :confused:
Guillermo
Posted

1 solution

Ignore what I wrote before, I misread your question :doh:

The problem is not with Enabled on your user control, but with group box. By default, setting Enabled propagates to all child controls. This seems to be done through the back door, as it were, and not by using the child controls' Enabled property. I tried creating a custom group box and shadowing its Enabled, but that didn't seem to work either.

I was able to come up with a solution for you, though. In your user control, implement a property called ReadOnly, like so:
VB
Private _ReadOnly As Boolean

<System.ComponentModel.DefaultValue(False)> _
Public Property [ReadOnly]() As Boolean
    Get
        Return _ReadOnly
    End Get
    Set(ByVal value As Boolean)
        _ReadOnly = value
        TextBox1.Enabled = Not _ReadOnly
    End Set
End Property

Public Sub New()
    InitializeComponent()
    _ReadOnly = False
End Sub

This will toggle the Enabled property of the textbox and leave the label alone. Note the use of square brackets around the property's name: this just tells the compiler to treat it as a label rather than a keyword. Now, create a subclass of GroupBox that also implements a ReadOnly property:
VB
Public Class MyGroupBox
    Inherits GroupBox

    Private _ReadOnly As Boolean

    Public Sub New()
        MyBase.New()
        _ReadOnly = False
    End Sub

    <System.ComponentModel.DefaultValue(False)> _
    Public Property [ReadOnly]() As Boolean
        Get
            Return _ReadOnly
        End Get
        Set(ByVal value As Boolean)
            _ReadOnly = value

            For Each Ctrl As Control In Me.Controls
                If Ctrl.GetType.GetProperty("ReadOnly") IsNot Nothing Then
                    Ctrl.GetType.GetProperty("ReadOnly").SetValue(Ctrl, _ReadOnly, Nothing)
                Else
                    Ctrl.Enabled = Not _ReadOnly
                End If
            Next
        End Set
    End Property

End Class

When the property is set, you iterate through all child controls. If the control has a ReadOnly property, that is what gets set. Otherwise, it will set the control's Enabled property.

My test case had a MyGroupBox control on a form, which contained both a MyUserControl control and a Label. A button toggled the ReadOnly property of the group box. Everything worked as expected.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900