Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. However I would also like raise an even whenever the value of my Text Property is changed and I want to call it 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.

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. 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. I don't understand how other people manage to do this but I would like to know how. Thanks.

like image 607
Landon Conway Avatar asked Oct 19 '25 04:10

Landon Conway


2 Answers

You should call the event handler delegate manually in the setting of the Text property.

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;
}
like image 78
Nico Avatar answered Oct 20 '25 18:10

Nico


If you just want to handle TextChanged event, you don't need to do anything, the UserControl class has TextChanged which works properly. But it's not browsable, like its Text property.

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:

  • How to: Implement Custom Event Accessors (C# Programming Guide)
  • How to: Handle Multiple Events Using Event Properties
like image 24
Reza Aghaei Avatar answered Oct 20 '25 18:10

Reza Aghaei



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!