简体   繁体   中英

set focus to textbox in user control c#

I created a user control in c# window forms say chatWindow and it has a textbox with butons. I placed this user control four times on MainForm say uc1, uc2, uc3, uc4. On MainForm I have also other controls. Now I want that when user press TAB then focus of uc1's textbox should be set. Pressing tab second time focus of uc2's textbox should be set and also for 3rd anf 4th tab.

I tried to set TabIndex of these user controls but could not get success. I dont know how to get user control's textbox property while in MainForm.

You can define a variable to store tab press count, define it inside of Form class in class level:

int count = 0;

Then in the Form's KeyDown event do the following:

private void Form1_KeyDown(object sender, KeyEventArgs e)
{
        if (e.KeyCode == Keys.Tab)
        {
            switch (count)
            {
                case 0:
                    this.ActiveControl = uc1TextBox;
                    count++;
                    break;
                case 1:
                    this.ActiveControl = uc2TextBox;
                    count++
                    break;

               // and so on...
            }
        }
}

first set tab orders and Ensure that all the controls you want to select have their TabStop property set to true

  private void Form1_KeyDown(object sender, KeyEventArgs e)
  {
    if (e.KeyCode == Keys.Enter)
    {
            e.Handled = true;
            this.ProcessTabKey(true);
    }

  }

Try Instead, Simply, In Design Mode, From properties Explorer set tabIndex value of uc1 to 0, uc2 to 1, uc3 to 2, uc4 to 3 and set their tabstop property true. Hope this is enough. Thanks.

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