Click here to Skip to main content
15,897,273 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a user control in window c# and i am adding that control into another project. I want to enable tabbing in all controls in it. User control contains multiple text boxes. When i entered value in first text box then automatically cursor should move to the next text box after the tab button pressed. But its moving to next control of the project. How can i enable this feature in it?
Posted
Updated 16-Oct-13 3:12am
v2

In general you need to implement your own event for tab pressed. In that event you should at first check, if tab should work for userControls, or inside one selected userControl. If it should work for userControls, you should call base method of tabbing. If it is for selected userControl, then you need to check what textbox is focused, and set focus to a next textbox. Good luck
 
Share this answer
 
v2
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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900