0
votes

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.

3
Are you just wanting to tab through the usercontrols ignoring the other controls on your form?Mark Hall
Why not consider setting the tabIndex?User2012384

3 Answers

3
votes

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...
            }
        }
}
0
votes

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);
    }

  }
0
votes

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.