简体   繁体   English

C#自定义TextChanged事件处理程序,用于UserControl的自定义Text属性?

[英]C# custom TextChanged event handler for custom Text property for a UserControl?

I created a custom User Control in C# and I added a custom Text property for my control. 我在C#中创建了一个自定义用户控件,并为该控件添加了一个自定义Text属性。 However I would also like raise an even whenever the value of my Text Property is changed and I want to call it TextChanged . 但是,无论何时我的Text属性的值更改,并且我想将其命名为TextChanged ,我都想提出一个偶数。

Here is my code for the property I created for my user control: 这是我为用户控件创建的属性的代码:

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;
    }
}

As you see I already created the TextChanged Event Handler but I don't know how to link it to the Text Property to make it to where when the value of 'Text' changes, the event will be raised. 如您所见,我已经创建了TextChanged事件处理程序,但是我不知道如何将其链接到Text属性,以使其到达“ Text”值更改时的位置,从而引发该事件。

Please be aware that I am using Windows Forms and I am not using WPF and I do not want to have to do anything that requires WPF. 请注意,我正在使用Windows窗体,并且没有在使用WPF,并且我不想做任何需要WPF的事情。 Every solution I have found for this issue has something to do with WPF or it does not completely solve my issue because they are not trying to create an event handler for a string. 我为此问题找到的每个解决方案都与WPF有关,或者由于他们没有尝试为字符串创建事件处理程序而不能完全解决我的问题。 I don't understand how other people manage to do this but I would like to know how. 我不了解其他人如何做到这一点,但我想知道如何做。 Thanks. 谢谢。

You should call the event handler delegate manually in the setting of the Text property. 您应该在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;
}

If you just want to handle TextChanged event, you don't need to do anything, the UserControl class has TextChanged which works properly. 如果您只想处理TextChanged事件,则无需执行任何操作, UserControl类具有TextChanged ,可以正常工作。 But it's not browsable, like its Text property. 但是它像它的Text属性一样不可浏览。

If you want to make it browsable, as an alternative to the other answer, you can use custom event accessors (event properties) this way: 如果要使其可浏览,作为其他答案的替代方法,可以通过以下方式使用自定义事件访问器(事件属性):

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

To learn more about the syntax take a look at this MSDN resource: 要了解有关语法的更多信息,请查看以下MSDN资源:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM