简体   繁体   中英

How can I give focus to any Control after a Tab has been selected

I have a problem .. I have an error list form (works as validation summary screen) that displays validation of controls that require to save data but have no values. This form opened when validation occurs on controls in another form that has tab control contains all controls that have validation.

The problem is when I double click on Error List form, I need cursor focus on tab control that have this control and focus on the control itself

The result : focus happened on tab control only .. but I need to focus on the control also

在选项卡中选择事件处理程序中使用Control.Focus()

调用Focus()专注于下一个控件。

Step 1 : You need to handle the Enter event of the TabPage Control to perform the operations when TabPage gains the focus.
Step 2: You can call Select() function on Required control to gain the Focus .

Try This: if you want to gain the Focus of TextBox control in TabPage2 use this code

  tabPage2.Enter += new System.EventHandler(this.tabPage2_Enter);
  private void tabPage2_Enter(object sender, EventArgs e)
    {
       textBox1.Select();
    }

I think the trick is to set socus on the tab page first, then set focus on the actual control you want to focus on.

What I was seeing is if the tab page was already selected setting focus to the control works fine. However, if the tab was programmatically activated then setting focus on the control alone does not work.

So this works for me reliably:

// first select and focus the tab
TabsResult.SelectedTab = tabRequest;
TabsResult.SelectedTab.Focus();

// then focus the control
txtRequestUrl.Focus();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM