Joe Barone
Joe Barone

Reputation: 3140

How do you return the focus to the last used control after clicking a button in a winform app?

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:

Sample App Screen Shot

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.

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:

   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();
        }


    }

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?

Upvotes: 7

Views: 12754

Answers (7)

Denis
Denis

Reputation: 12077

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.

Upvotes: 0

eric
eric

Reputation:

You could do the following

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.

Upvotes: 2

Joe Barone
Joe Barone

Reputation: 3140

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.

Upvotes: 0

Joshua Turner
Joshua Turner

Reputation: 1049

For a bit of 'simplicity' maybe try.

public Form1()
    {
        InitializeComponent();

        foreach (Control ctrl in Controls)
        {
            if (ctrl is TextBox)
            {
                ctrl.Enter += delegate(object sender, EventArgs e)
                              {
                                  _lastEnteredControl = (Control)sender;
                              };
            }
        }
    }

then you don't have to worry about decorating each textbox manually (or forgetting about one too).

Upvotes: 16

Dan Herbert
Dan Herbert

Reputation: 103417

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 GotFocus 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.

private void Form_OnLoad(object obj, EventArgs e)
{
    AddGotFocusListener(this);
}

private void AddGotFocusListener(Control ctrl)
{
    foreach(Control c in ctrl.Controls)
    {
        c.GotFocus += new EventHandler(Control_GotFocus);
        if(c.Controls.Count > 0)
        {
            AddGotFocusListener(c);
        }
    }
}

private void Control_GotFocus(object obj, EventArgs e)
{
    // Set focused control here
}

Upvotes: 1

Ethan Gunderson
Ethan Gunderson

Reputation: 11507

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.

Upvotes: 1

Jon Limjap
Jon Limjap

Reputation: 95432

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?

I'm interested in finding out why you want to do this in the first place.

Upvotes: 0

Related Questions