Chrayfish
Chrayfish

Reputation: 157

How can I pass an event from one class to a child object?

I have a class called TextBoxPlus that inherits from UserControl and holds multiple regular TextBox instances, while also exapnding on their functionality. I have an instace of TextBoxPlus in my Windows Form that is calling the TextChanged event. The issue I have is I want it to pass through to the TextChanged event of one of the TextBox instances within the TextBoxPlus. I don't want to make the TextBox public so I'm likely looking at an override but I'm not sure on how to do this as this does not work:

protected override void TextChanged(EventArgs e)
{
    this.textbox.TextChanged(e);
}

obviously due to the TextChanged event not calling a method. How can I achieve this?

Upvotes: 0

Views: 151

Answers (1)

Servy
Servy

Reputation: 203850

Define an event on the containing class whose implementation adds its handlers to the contained object's events:

public event  EventHandler TextChanged
{
    add
    {
        textbox.TextChanged += value;
    }
    remove
    {
        textbox.TextChanged -= value;
    }
}

Upvotes: 2

Related Questions