Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I control the focus order for TextBoxes inside a panel?

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:

an image to illustrate my point

like image 827
Agnieszka Polec Avatar asked Jul 26 '14 00:07

Agnieszka Polec


3 Answers

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.

enter image description here

like image 52
Shell Avatar answered Oct 07 '22 01:10

Shell


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 TabIndexes 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 UserControls whenever possible. If you reuse parts of your UI with good design of UserControls, 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.

like image 41
Dave Cousineau Avatar answered Oct 06 '22 23:10

Dave Cousineau


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.

like image 38
Troyen Avatar answered Oct 07 '22 00:10

Troyen