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

一世执手 提交于 2020-01-21 06:37:11

问题


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:


回答1:


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.




回答2:


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.




回答3:


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.




回答4:


From MSDN - Control.TabIndex:

A tab index can consist of any valid integer greater than or equal to zero, lower numbers being earlier in the tab order. If more than one control on the same parent control has the same tab index, the z-order of the controls determines the order to cycle through the controls.

For a control to be included in the tab order, its TabStop property must be set to true.

Accordingly,

textbox1.TabIndex = 1; // and do the same for each one in the desired order
textbox1.TabStop = true;



回答5:


You need to set the TabIndex property of the TextBox Controls.

From MSDN : Control.TabIndex

Gets or sets the tab order of the control within its container.

For a control to be included in the tab order, its TabStop property must be set to true.

Try This: (Example)

txtTextBox1.TabIndex = 1;
txtTextBox2.TabIndex = 2;
txtTextBox3.TabIndex = 3;

in the above example Tab Focus order is as below:

txtTextBox1
txtTextBox2
txtTextBox3

Note: You need to make sure that TabStop property of the textbox controls are set to True otherwise tab ordering does not work,but by default when you are designing controls using Visual Studio IDE(using drag and drop functionality) TabStop property is set to True.

From MSDN : Control.TabStop

Gets or sets a value indicating whether the user can give the focus to this control using the TAB key.

Try This: Setting TabStop property

txtTextBox1.TabIndex = 1;
txtTextBox1.TabStop = True;

txtTextBox2.TabIndex = 2;
txtTextBox2.TabStop = True;

txtTextBox3.TabIndex = 3;
txtTextBox3.TabStop = True;


来源:https://stackoverflow.com/questions/24966212/how-do-i-control-the-focus-order-for-textboxes-inside-a-panel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!