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

后端 未结 7 1216
忘掉有多难
忘掉有多难 2021-01-02 23:23

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 butt

7条回答
  •  一整个雨季
    2021-01-02 23:57

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

提交回复
热议问题