Winforms c# - Set focus to first child control of TabPage

前端 未结 6 1452
醉酒成梦
醉酒成梦 2020-12-29 06:03

Say I have a Textbox nested within a TabControl.

When the form loads, I would like to focus on that Textbox (by default the

相关标签:
6条回答
  • 2020-12-29 06:26

    I had a user control within another user control. textbox1.Select() worked for me but textbox1.Focus() did not work.

    You can also try setting Tabstop to false, textbox1.Focus(), TabStop true.

    0 讨论(0)
  • 2020-12-29 06:32

    The following is the solution:

    private void frmMainLoad(object sender, EventArgs e)
    {
        ActiveControl = textBox1;
    }
    

    The better question would however be why... I'm not entirely sure what the answer to that one is.

    Edit: I suspect it is something to do with the fact that both the form, and the TabControl are containers, but I'm not sure.

    0 讨论(0)
  • 2020-12-29 06:43
      private void ChildForm1_Load(object sender, EventArgs e)
            {
                ActiveControl = txt_fname;
    
            }
    

    i use this code it works fine on win tab control or dotnetbar supertab contrl

    0 讨论(0)
  • 2020-12-29 06:48

    Try putting it in the Form_Shown() event. Because it's in a container, putting in the Form_Load or even the Form() constructor won't work.

    0 讨论(0)
  • Try to use textbox1.Select() instead of textbox1.Focus(). This helped me few times.

    0 讨论(0)
  • 2020-12-29 06:51

    You just need to add the Control.Select() for your control to this code. I have used this to set focus on controls during validation when there are errors.

    private void ShowControlTab(Control ControlToShow)
        {
            if (!TabSelected)
            {
                if (ControlToShow.Parent != null)
                {
                    if (ControlToShow.Parent.GetType() == typeof(TabPage))
                    {
                        TabPage Tab = (TabPage)ControlToShow.Parent;
                        if (WOTabs.TabPages.Contains(Tab))
                        {
                            WOTabs.SelectedTab = Tab;
                            TabSelected = true;
                            return;
                        }
                    }
    
                    ShowControlTab(ControlToShow.Parent);
                }
            }
        }
    
    0 讨论(0)
提交回复
热议问题