TabIndex: [
^]
TabStop: [
^]
PreviewKeyDown Event: [
^]
SelectNextControl Method: [
^]
When you create a UserControl, and put TextBoxes into it at
design-time by drag-drop from the ToolBox, by default, the TextBox AcceptsTab property will be 'false, and the 'TabStop property will be 'true.
That means you should expect that when you place an instance of that UserControl on a Form, by drag-drop, from the ToolBox, that its run-time behavior will be "standard:" pressing the Tab Key when a TextBox has Focus, and the insertion-cursor is within it, will move to the next Control in the TabOrder.
If the above is the scenario you are using, then check to see if your TextBoxes have their TabStop property set to 'true, and their AcceptsTab property set to 'false.
If you are adding TextBoxes to a Form, or UserControl,
at run-time, in code: whatever Control you add is going to get a TabIndex assigned greater than the highest TabIndex currently used in its containing Form/UserControl.
So you should expect that pressing the Tab Key in the last TextBox added to the UserControl at run-time to "tab out" of the UserControl.
And, if you don't want that to happen, then you need to directly manipulate the TabIndex property of the UserControl's Control Collection to force the TabOrder you want.
Or, you will need to handle Tab Key events yourself. Since many Controls, like TextBox, have their own automatic behaviors in response to the Tab Key, you'll have to use the PreviewKeyDown Event to get Tab Key presses. Example:
private void textBox1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
if(e.KeyCode == Keys.Tab) e.IsInputKey = true;
}
By setting the 'InputKey property to 'true when a Tab Key Press occurred, you have now enabled the 'KeyDown EventHanlder (as well as the KeyPress EventHandler) to "trap" Tab Key Events.
At this point, if you want to "lock" the user in to moving only between TextBoxes in the same container, you can use:
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Tab)
{
SelectNextControl((sender as TextBox), true, true, false, true);
}
}
This means the end-user will have to manually move the Focus outside of the Container Control in which a group of TextBoxes are sited to use Tab to move to other Controls on the Form.
If there are TextBoxes you don't want "tabbed to," then just set their 'TabStop property to 'false.