点击一个winform应用程序按钮后,你如何将焦点返回到上次使用的控制? [英] How do you return the focus to the last used control after clicking a button in a winform app?

查看:661
本文介绍了点击一个winform应用程序按钮后,你如何将焦点返回到上次使用的控制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个Windows窗体应用程序(C#),其中一个用户在表单中输入的数据。在任何时候,在编辑表单中的数据,用户可以点击一个按钮的形式来执行某些操作。默认情况下,焦点转移到点击的按钮,使用户可以单击后退到他们想要编辑以继续修改表单上的数据的控制。我需要能够做的是将焦点返回到最后一个编辑控件后按钮单击事件已被处理。下面是一个示例屏幕截图,说明了什么我说的:

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:

用户可以在TextBox1中,TextBox2中,textbox3等输入数据,然后单击按钮。我需要的按钮返回焦点返回到最近具有焦点的按钮被点击之前的控制。

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


    }

所以基本上我们在这里是一个类变量指向最后输入的控制。在窗体上的每个文本框的安装程序,以便在控件接收焦点textBox_Enter方法​​被激发。然后,单击该按钮时,焦点将返回到具有焦点的按钮被点击之前的控制。任何人有任何更优雅的解决方案呢?

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?

推荐答案

有关有点简单也许尝试。

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

这篇关于点击一个winform应用程序按钮后,你如何将焦点返回到上次使用的控制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆