I have a Form with many TextBoxes. I need some TextBoxes inside one group, and other text boxes inside another group. By group, I just need a way to make these TextBoxes appear to belong with each other.
I made two Panels and added the TextBoxes to them. Then, I placed a border around these Panels.
However, my problem is that when I press Tab, the focus doesn't go to the next TextBox, but rather it goes in a random order to another TextBox. Sometimes the next TextBox is inside the first Panel, other times it is in the second Panel. How can I control the focus order?
This is an image to illustrate my point:
Tab order should be set like this. Both top container panel should have TabIndex 0 and 1 respectively and its child control should have TabIndex prefix by their parent control Tab Index. ie. if the Panel1 has TabIndex 0 then its child controls should have TabIndex 0.0,0.1,0.2,0.3...
NOTE: make sure that if the Tab Stop property of any control is set to false then cursor will not move into that control. In that case TabIndex will not work.
As others have said, use the TabIndex
property to indicate the tabbing order, and the TabStop
property to determine whether or not the control can be tabbed to at all.
However, there is a much simpler way to do this from the designer. When looking at your form in the designer, make sure your form is selected (and not a control on the form) (you can do this by clicking once in the whitespace around the form), and then choose View -> Tab Order.
With the tab order designer active, you will see the TabIndex
es of every control. Click them in the order you would like to be able to tab through them. The TabIndex
tooltips will change from blue to white as you assign them. When you are done, choose View -> Tab Order again to return to the normal designer state.
Another thing to mention would be to suggest using UserControl
s whenever possible. If you reuse parts of your UI with good design of UserControl
s, you can avoid having a form with dozens and dozens of tab stops to assign, since each UserControl
will have its own internal tabbing order that will automatically apply when placed on a Form, and you will only have to set the TabIndex
of the UserControl
itself.
When you select a textbox on your designer, you should see a property for the textbox called the TabIndex. When tabbing through controls, focus goes to the component with the next highest TabIndex.
You'll want to set the TabIndex for each of the boxes so that tabbing rotates through the boxes in the order that you expect.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With