0

I have a bunch of TextBoxes in my WinForm UserControl. Each of those text boxes has few event handlers such as On_Enter - show ListBox with suggestions, On_KeyUP - if Keys.Code == Keys.Enter - SelectNextControl(). When I place that control in a Form, none of those events fire up. How to expose all those events to the containing form? How to make UserControl's events fire up event handlers of that UserControl?

ArtK
  • 1,157
  • 5
  • 17
  • 31
  • @valter - this is an abstract question, code would be irrelevant – ArtK Sep 18 '14 at 18:28
  • I think the reason you haven't got any responses to your question so far is because most people answering here like to see an attempt at finding a solution before they answer. Personally, if I understand you right, I think your question is not really abstract, but is a common practical issue. It would have been nice if you posted some code to show what you tried. Anyway, I will try and answer this for you. – Ben Nov 19 '14 at 16:33

1 Answers1

2

So, if I understand correct, I think there are 2 ways you can proceed:

Approach 1

In the UserControl, set the Modifiers property of each textbox (or the ones you are interested in) to public:

enter image description here

Then in the Form that uses this UserControl you can access all these textboxes and hence their events:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        myUserControl1.textBox1.KeyDown += new KeyEventHandler(textBox1_KeyDown);
    }
}

Approach 2 (Taken from this post)

You can create new events for your UserControl that simply pass forward the event of an underlying textbox. The underlying textboxes can then remain private to the UserControl.

In the UserControl add this event:

public event KeyEventHandler TextBox1KeyDown
{
    add { textBox1.KeyDown += value; }
    remove { textBox1.KeyDown -= value; }
}

Or you can create a single event that deals with all textboxes:

public event KeyEventHandler AnyTextBoxKeyDown
{
    add
    {
        textBox1.KeyDown += value;
        textBox2.KeyDown += value;
        textBox3.KeyDown += value;
        ...
    }
    remove
    {
        textBox1.KeyDown -= value;
        textBox2.KeyDown -= value;
        textBox3.KeyDown -= value;
        ...
    }
}

Now your UserControl has a event of its own that the code in the Form can use:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        myUserControl1.TextBox1KeyDown += new KeyEventHandler(myUserControl1_TextBox1KeyDown);
        myUserControl1.AnyTextBoxKeyDown += new KeyEventHandler(myUserControl1_AnyTextBoxKeyDown );
    }

    private void myUserControl1_TextBox1KeyDown(object sender, KeyEventArgs e)
    {
        /* We already know that TextBox1 was pressed but if
         * we want access to it then we can use the sender
         * object: */
        TextBox textBox1 = (TextBox)sender;

        /* Add code here for handling when a key is pressed
         * in TextBox1 (inside the user control). */
    }

    private void myUserControl1_AnyTextBoxKeyDown(object sender, KeyEventArgs e)
    {
        /* This event handler may be triggered by different
         * textboxes. To get the actual textbox that caused
         * this event use the following: */
        TextBox textBox = (TextBox)sender;

        /* Add code here for handling when a key is pressed
         * in the user control. */
    }
}

Note that while this approach keeps the textboxes private within the UserControl, they can still be accessed from the event handler by the sender argument.

Community
  • 1
  • 1
Ben
  • 3,241
  • 4
  • 35
  • 49
  • thanx so much for your answer. However, I have 2 more questions. What type is myUserControl1_AnyTextBoxKeyDown and where should be declared? I'm assuming it is a 'delegate' but not quite sure where that definition should go. – ArtK Jan 22 '15 at 21:35
  • @ArtK, `myUserControl1_AnyTextBoxKeyDown` is a method that is used to handle the event. If you are using Visual Studio then after you create the new event for your user control (in this example `AnyTextBoxKeyDown` event) it should appear in the intielisence, and as you type the " += new " it should offer to fill in the rest for you. If you let it fill in the rest then it will automatically create the new method as well. This should go in the form class that has the user control. – Ben Jan 23 '15 at 11:47
  • In my example I put the event registration in the Form constructor and this is usually the best place, but sometimes you may want to put this somewhere else depending on the requirements. – Ben Jan 23 '15 at 11:53
  • @ArtK I have updated my answer to show the event methods. I hope this helps. – Ben Jan 23 '15 at 12:09