<p>I'm working on a windows forms application (C#) where a user is entering data in a form.  At any point while editing the data in the form the user can click one of the buttons on the form to perform certain actions.  By default the focus goes to the clicked button so the user has to click back on to the control they want to edit in order to continue modifying the data on the form.  What I need to be able to do is return the focus to the last edited control after the button click event has been processed.  Here's a sample screenshot that illustrates what I'm talking about:</p>

<p><img src="https://lh6.ggpht.com/joe.r.barone/SLs4KZMBqfI/AAAAAAAABNw/P6xtqhCo8Y4/s800/SampleApp1.jpg" alt="Sample App Screen Shot"></p>

<p>The user can be entering data in textbox1, textbox2, textbox3, etc and click the button.  I need the button to return the focus back to the control that most recently had the focus before the button was clicked.</p>

<p>I'm wondering if anyone has a better way of implementing this functionality than what I've come up with.  Here's what I'm doing right now:</p>

<pre><code>   public partial class Form1 : Form
    {
        Control _lastEnteredControl;

        private void textBox_Enter(object sender, EventArgs e)
        {
            _lastEnteredControl = (Control)sender;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Do something here");
            _lastEnteredControl.Focus();
        }


    }
</code></pre>

<p>So basically what we have here is a class variable that points to the last entered control.  Each textbox on the form is setup so the textBox_Enter method is fired when the control receives the focus.  Then, when the button is clicked focus is returned to the control that had the focus before the button was clicked.  Anybody have any more elegant solutions for this?</p>

[{'answer_id': 37319, 'author': 'Jon Limjap', 'author_id': 372, 'author_profile': 'https://Stackoverflow.com/users/372', 'pm_score': 0, 'selected': False, 'text': '<p>Your implementation looks good enough -- what I do want to know is why you want to do this in the first place? Won\'t it be preferrable for the focus to cycle back to the first entry? Is the data in the last text box so malleable that once they click the button it is "remembered"? Or do you have some sort of operation that the button does to that specifici text box data -- in that case shouldn\'t the focus go to a subsequent control instead?</p>\n\n<p>I\'m interested in finding out why you want to do this in the first place.</p>\n'}, {'answer_id': 37320, 'author': 'Ethan Gunderson', 'author_id': 2066, 'author_profile': 'https://Stackoverflow.com/users/2066', 'pm_score': 1, 'selected': False, 'text': "<p>I think what you're doing is fine.  The only thing I could think of to improve it would be to store each control into a stack as they are accessed.  That would give you a complete time line of what was accessed.</p>\n"}, {'answer_id': 37331, 'author': 'Dan Herbert', 'author_id': 392, 'author_profile': 'https://Stackoverflow.com/users/392', 'pm_score': 1, 'selected': False, 'text': '<p>Your approach looks good. If you want to avoid having to add an the event handler to every control you add, you could create a recursive routine to add a <a href="http://msdn.microsoft.com/en-us/library/system.windows.forms.control.gotfocus.aspx" rel="nofollow noreferrer">GotFocus</a> listener to every control in your form. This will work for any type of control in your form, however you could adjust it to meet your needs.</p>\n\n<pre><code>private void Form_OnLoad(object obj, EventArgs e)\n{\n    AddGotFocusListener(this);\n}\n\nprivate void AddGotFocusListener(Control ctrl)\n{\n    foreach(Control c in ctrl.Controls)\n    {\n        c.GotFocus += new EventHandler(Control_GotFocus);\n        if(c.Controls.Count &gt; 0)\n        {\n            AddGotFocusListener(c);\n        }\n    }\n}\n\nprivate void Control_GotFocus(object obj, EventArgs e)\n{\n    // Set focused control here\n}\n</code></pre>\n'}, {'answer_id': 37336, 'author': 'Joshua Turner', 'author_id': 820, 'author_profile': 'https://Stackoverflow.com/users/820', 'pm_score': 5, 'selected': True, 'text': "<p>For a bit of 'simplicity' maybe try.</p>\n\n<pre><code>public Form1()\n    {\n        InitializeComponent();\n\n        foreach (Control ctrl in Controls)\n        {\n            if (ctrl is TextBox)\n            {\n                ctrl.Enter += delegate(object sender, EventArgs e)\n                              {\n                                  _lastEnteredControl = (Control)sender;\n                              };\n            }\n        }\n    }\n</code></pre>\n\n<p>then you don't have to worry about decorating each textbox manually (or forgetting about one too).</p>\n"}, {'answer_id': 37338, 'author': 'Joe Barone', 'author_id': 3452, 'author_profile': 'https://Stackoverflow.com/users/3452', 'pm_score': 0, 'selected': False, 'text': "<p>Yeah, I admit the requirement is a bit unusual.  Some of the information that the users will be entering into this application exists in scans of old documents that are in a couple of different repositories.  The buttons facilitate finding and opening these old docs.  It's difficult to predict where the users will be on the form when they decide to pull up a document with more information to enter on the form.  The intent is to make the UI flow well in spite of these funky circumstances.</p>\n"}, {'answer_id': 269411, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 2, 'selected': False, 'text': "<p>You could do the following</p>\n\n<p>Change the button to a label and make it look like a button.  The label will never get focus and you don't have to do all the extra coding.  </p>\n"}, {'answer_id': 24293401, 'author': 'Denis', 'author_id': 400589, 'author_profile': 'https://Stackoverflow.com/users/400589', 'pm_score': 0, 'selected': False, 'text': '<p>Create a class called CustomTextBox that inherits from TextBox. It has a static variable called stack. When the textbox loses focus push onto the stack. When you want to find the last focused control then just pop the first item from the stack. Make sure to clear the static Stack variable.</p>\n'}]
['https://Stackoverflow.com/questions/37317', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3452/']