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