在自定义控件中启用标签 [英] Enable Label inside a custom control

查看:66
本文介绍了在自定义控件中启用标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个继承自System.Windows.Forms.UserControl的控件.假设我的控件包含一个标签和一个文本框.我在组框内的窗体中使用控件.当我禁用组框时,控件的标签和文本框都被禁用.我试图使标签保持启用状态,但我不能启用.
我试过了:
1.重写WndProc方法,捕获WM_ENABLE消息并在此时禁用文本框(如果LPARAM为true),最终启用标签
2.在调用Mybase之后,重写Sub OnEnabledChanged. OnEnabledChanged,设置标签.Enabled为true
3.创建一个公共重载属性Enabled(),然后在此处将TextBox.Enabled属性设置为正确的值,并保持Label为启用状态
4.处理Me.EnabledChanged或Mybase.EnabledChanged
5.将一个面板添加到我的控件中,并包括标签和文本框.然后
5.1.处理面板的OnEnabledChange,在此方法中启用标签.
5.2.在之前我尝试启用标签的四个地方中的任何一个处启用面板(尚未启用#1),其想法是启用容器可以让我管理各个控件.

我一直认为这个想法一定很容易实现,但事实并非如此.有人可以帮忙吗? :confused:
Guillermo

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

推荐答案

忽略我之前写的内容,我读错了你的问题:doh:

问题不在于用户控件上的Enabled,而是与组框有关.默认情况下,设置Enabled会传播到所有子控件.这似乎是通过后门完成的,而不是通过使用子控件的Enabled属性来完成的.我尝试创建一个自定义组框并隐藏其Enabled,但这似乎也不起作用.

不过,我能够为您提供解决方案.在您的用户控件中,实现一个名为ReadOnly的属性,如下所示:
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


这将切换文本框的Enabled属性,并保留标签.请注意,在属性名称的周围使用方括号:这只是告诉编译器将其视为标签而不是关键字.现在,创建GroupBox的子类,该子类也实现ReadOnly属性:


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


设置属性后,您将遍历所有子控件.如果控件具有ReadOnly属性,则将设置该属性.否则,它将设置控件的Enabled属性.

我的测试用例在表单上有一个MyGroupBox控件,其中包含一个MyUserControl控件和一个Label.一个按钮可以切换组框的ReadOnly属性.一切都按预期进行.


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.


这篇关于在自定义控件中启用标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆