13

so I have started from 0 and defining tabindex for the controls on my form but at run time it is all messed up. the form is a little complex tho. it has horizontal and vertical splitters and panels, group boxes and some older VB 6.0 activeX controls which is a Tree control inside them. even if i do it programmatically and read previewkeydown eventg and say if it is TAB then control2.Focus() it is still working wrong. so frustrating. any thoughts? ..there are also labels on the form which do not need tab so I have defined 0 for their index.

Bohn
  • 26,091
  • 61
  • 167
  • 254
  • Found this [Documentation](https://msdn.microsoft.com/en-us/library/bd16a8cw(v=VS.80).aspx) from Microsoft & Stackoverflow another [Question](http://stackoverflow.com/questions/11052265/tabindex-does-not-work-correctly) is very useful. – RajeshKdev May 28 '15 at 10:16

4 Answers4

26

How are you setting it? If you are in visual studio with the form in design view select view -> tab order and then click on each item in the order you want them.

Usually works for me.

kerry
  • 556
  • 5
  • 19
  • thanks. i will try it this way too. I was setting through the properties of each control. – Bohn May 26 '11 at 14:07
  • 1
    oh my god! it is complicated. they show like 0.1.0.0.10 and 0.1.0.1.11 – Bohn May 26 '11 at 14:09
  • 3
    The different numbers are the different layers of controls. You need to select the highest level and then work in. Each layer will have a number in the top left corner select this then the controls in that layer in the order required – kerry May 26 '11 at 14:49
  • This! View -> TabOrder saved a lot of time and more frustration. What I had to change was also the TabIndex of the different levels of panels I had. – snakepitbean May 15 '15 at 21:01
  • In general a nice function. BUT. I have a really small form with a lot of controls. I cant see them because of the long labels like `0,0,0,0,0,0,0,0,0,0,0,5`. – C4d Aug 18 '15 at 09:57
  • 2
    Please note that even using the form designer, it get's a little funky when you have nested controls. It's not enough to just use the designer tab-order to select the order of controls ... you ALSO need to select containing controls (eg panels etc) - otherwise it often won't work as expected. – Chris Rogers Sep 26 '16 at 23:33
  • Excellent! I've been working for years without knowing this feature in VS, thanks a lot – Matias Dec 19 '18 at 20:22
14

The reason is that the controls are in different Containers. Suppose you've got panel1.TabIndex = 0 and panel2.TabIndex = 1, then in panel2, textBox1.TabIndex = 0, in panel1, textBox2.TabIndex = 1. At runtime, textBox1 comes before textBox2 because its panel comes first!

As kerry said, use view->tab order to see the complete hierarchy of tab orders.

Alireza
  • 5,421
  • 5
  • 34
  • 67
  • Exactly, the fourth numbers (counting starts from one yeah? ;) is 0 in one and 1 in the other. So the two controls are in different containers. And this is causing the problem. – Alireza Jan 10 '15 at 14:39
2

I'm mentioning this because I haven't seen it in any of the winforms tab order threads that I have found on stackoverflow.

If you have multiple panels, you change your panel tab order by clicking on the Panel, going to properties, and then you change the TabIndex to whatever you want. This will allow you to navigate from panel to panel in the order that you want. Then within each panel, follow the recommended steps listed above using view > tab order and click on each cell in the order that you want to set.

noel
  • 383
  • 4
  • 18
0

Follow the steps below:

  1. Set the TabIndex property to DIRECT CHILD containers and controls in your form or container, either using the View > TabOrder utility or directly from the properties window. Completely ignore the TabStop property of containers, which defaults to false even it's very important.
  2. Repeat step 1 with each container.
Mario Vázquez
  • 717
  • 10
  • 9