Creating custom Event for a custom control

EFileTahi-A

Contributor
Joined
Aug 8, 2004
Messages
623
Location
Portugal / Barreiro
I need o trigger events for my custom properties residing in a custom control.

Basically, I need a trigger for each property the control has and whenever they change. I've seen some examples on the web but I did not get a clear picture of how the whole thing works.

This is what I have so far:

Code:
public partial class EFile_ComboBox : UserControl
{
        public event EventHandler TextChanged;

        [Category("Appearance")]
        [Description("Specifies if a shadow should be draw for the button text.")]
        [DisplayName("Text")]
        [EditorBrowsable(EditorBrowsableState.Always), Browsable(true),
         DesignerSerializationVisibility(DesignerSerializationVisibility.Visible),
         Bindable(true)]
        public override string Text
        {
            get
            {
                return sText;
            }
            set
            {
                sText = value;
                this.Invalidate();
            }
        }

        private void TextBox_Validated(object sender, EventArgs e)
        {
            // invoke UserControl event here
            if (this.TextChanged != null) this.TextChanged(sender, e);
        }
}

What should I do from here? The Text property, in this case, is the one thing I need to trigger whenever it changes.
 
Last edited:
I'm not clear on what you're asking. You want to raise the TextChanged event when the Text property is set? You already have code that raises the event. Just do the same in the Text setter. Or am I misunderstanding?
Code:
        public override string Text
        {
            get
            {
                return sText;
            }
            set
            {
                sText = value;
                this.Invalidate();

                var evnt = TextChanged;
                if(evnt != null) evnt(this, EventArgs.Empty);
            }
        }
 
I want the event to be fired when the user changes the text on the control based on a custom property created by me. The current implementation does not fire anything. I don't know if I'm making myself clear.

I have a Text property being override by me so I can force the Invalidate() instruction. Now, this control is a custom made combobox. When the user chooses an item (through a context menu) the Text property gets the value of the selected item. And it is at this time I want to trigger an event for the changed text. Don't I need to create a delegate for the whole thing? I'm really not sure of how the whole thing works.

Thank you for replying!
 
No, you shouldn't need to create a delegate type. The code I posted should be all you need to raise your event. Here is a more complete listing:
Code:
public partial class EFile_ComboBox : UserControl
{
        public event EventHandler TextChanged;

        [Category("Appearance")]
        [Description("Specifies if a shadow should be draw for the button text.")]
        [DisplayName("Text")]
        [EditorBrowsable(EditorBrowsableState.Always), Browsable(true),
         DesignerSerializationVisibility(DesignerSerializationVisibility.Visible),
         Bindable(true)]
        public override string Text
        {
            get
            {
                return sText;
            }
            set
            {
                sText = value;
                this.Invalidate();

[COLOR="Blue"]                var evnt = TextChanged;
                if(evnt != null) evnt(this, EventArgs.Empty);
[/COLOR]            }
        }

        private void TextBox_Validated(object sender, EventArgs e)
        {
[COLOR="Red"]            // invoke UserControl event here
            if (this.TextChanged != null) this.TextChanged(sender, e);[/COLOR]
        }
}
The code in blue I added, which raises the TextChanged event. The code in red I'm a bit confused about. It also raises the TextChanged event, but I don't see why you would raise this event anywhere except the Text property setter. If there is a reason the event needs to be raised from the TextBox_Validated method instead of the Text setter, then your code should work as-is, without my addition. Has the code you posted not been working as expected?
 
My bad! I had a hunch I was doing something wrong.

The TextChanged event was not firing because I was setting the item's text directly to the sText variable instead of loading it to the this.Text proterty, whenever I clicked on the context box's items.

Thanks SnarfBlam, once again. Thanks to you I now realize how the event thing works.
 
Back
Top