【问题标题】:C# custom TextChanged event handler for custom Text property for a UserControl?用于 UserControl 的自定义 Text 属性的 C# 自定义 TextChanged 事件处理程序?
【发布时间】:2016-08-16 02:55:38
【问题描述】:

我在 C# 中创建了一个自定义用户控件,并为我的控件添加了一个自定义文本属性。但是,每当我的 Text 属性的值发生更改并且我想将其称为 TextChanged 时,我也想提高一个偶数。

这是我为我的用户控件创建的属性的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TestControls
{
    public partial class Box: UserControl
    {
        public Box()
        {
            InitializeComponent();
        }

        [Bindable(true)]
        [EditorBrowsable(EditorBrowsableState.Always)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        [Browsable(true)]
        [Category("Appearance")]
        public override string Text { get { return base.Text; } set { base.Text = value; this.Invalidate(); } }

        [Browsable(true)]
        public event EventHandler TextChanged;
    }
}

如您所见,我已经创建了 TextChanged 事件处理程序,但我不知道如何将其链接到 Text 属性,以便在“文本”的值发生更改时将其连接到将引发事件的位置.

请注意,我使用的是 Windows 窗体,而我没有使用 WPF,我不想做任何需要 WPF 的事情。我为这个问题找到的每个解决方案都与 WPF 有关,或者它不能完全解决我的问题,因为他们没有尝试为字符串创建事件处理程序。我不明白其他人是如何做到这一点的,但我想知道如何做到这一点。谢谢。

【问题讨论】:

    标签: c# winforms user-controls event-handling


    【解决方案1】:

    您应该在Text 属性的设置中手动调用事件处理程序委托。

    public partial class Box : UserControl
    {
        public Box()
        {
            InitializeComponent();
        }
    
        [Bindable(true)]
        [EditorBrowsable(EditorBrowsableState.Always)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        [Browsable(true)]
        [Category("Appearance")]
        public override string Text
        {
            get { return base.Text; }
            set
            {
                if (base.Text != value)
                {
                    base.Text = value; this.Invalidate();
                    if(TextChanged != null)
                        TextChanged(this, EventArgs.Empty)
                }
            }
        }
    
        [Browsable(true)]
        public event EventHandler TextChanged;
    }
    

    【讨论】:

    • 你不知道你刚刚帮助了我多少。非常感谢。
    • 如果你只想处理TextChanged事件,你不需要做任何事情,UserControl类有TextChanged可以正常工作。但它不可浏览,就像它的 Text 属性一样。如果您想使其可浏览,作为替代方案,您可以使用自定义事件访问器(事件属性)。
    【解决方案2】:

    如果你只想处理TextChanged 事件,你不需要做任何事情,UserControl 类有TextChanged 可以正常工作。但它不可浏览,就像它的 Text 属性一样。

    如果您想使其可浏览,作为其他答案的替代方案,您可以通过这种方式使用自定义事件访问器(事件属性):

    [Browsable(true)]
    public event EventHandler TextChanged
    {
        add { base.TextChanged += value; }
        remove { base.TextChanged -= value; }
    }
    

    要了解有关语法的更多信息,请查看此 MSDN 资源:

    【讨论】:

    • 同样在自定义Text属性时,而不是在setter中调用this.Invalidate();,这种自定义的一个不错的选择是将它们放在OnTextChanged覆盖中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-16
    • 2010-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多