Focusing on the control when switching tabs

What I want to do is set the focus to a specific control (in particular TextBox) on the tab when this page is selected.

I tried to trigger Focusduring the Selected event containing the tab control, but this does not work. After that, I tried to call focus during the event of VisibleChangedthe control itself (with a check that I did not focus on the invisible control), but this also does not work.

Searching on this site, I came across this question , but this also does not work. Although after that I noticed that calling the Focuscontrol does it ActiveControl.

+3
source share
2 answers

I did this and it seems to work:

Refer to SelectedIndexChangedfor tabControl. Make sure tabControl1.SelectedIndex== the one i want and calltextBox.Focus();

I am using VS 2008, BTW.


Something like this worked:

private void tabControl1_selectedIndexChanged(object sender, EventArgs e)
{
   if (tabControl1.SelectedIndex == 1)
   {
      textBox1.Focus();
   }
}
+8
source

Try TabPage.Enter something like

        private void tabPage1_Enter (object sender, EventArgs e)
        {
            TabPage page = (TabPage) sender;
            switch (page.TabIndex)
            {
                case 0:
                    textBox1.Text = "Page 1";
                    if (! textBox1.Focus ())
                        textBox1.Focus ();

                    break;
                case 1:
                    textBox2.Text = "Page 2";

                    if (! textBox2.Focus ())
                        textBox2.Focus ();

                    break;
                default:
                    throw new InvalidOperationException();
            }
        }
+1

Source: https://habr.com/ru/post/1697999/


All Articles