VBForums >
.NET >
C# > [RESOLVED] Do panels mess up Focus for other controls?
Click to See Complete Forum and Search --> : [RESOLVED] Do panels mess up Focus for other controls?
Kasracer
Jan 29th, 2006, 06:39 PM
I have an odd situation. Unfortuantely I can't post my code, however I can explain exactly what I did.
At first I had a form with 1 label next to a text box at the top, a larger text box in the middle, and 2 buttons at the bottom (save and cancel). I had the first textbox's TabIndex set to 0, the middle TextBox's to 1, Save to 2, and cancel to 3.
This worked great and worked as expected.
Then I wanted to make it easily resizable. I took 2 panels. 1 Panel I placed the top label and text box onto it and set the Dock to Top. The second label I took and placed the 2 buttons onto it (save and cancel) and placed the panel at the bottom of the form with a Dock of Bottom. The center Textbox didn't work correctly with a Dock of Fill (it went under the panel controls) so I placed some simple code in the resize event of the form to manually resize it.
All works well except now the first item that has focus is the center textbox (which has a TabIndex of 1).
I don't understand why.
Both panels have have TabStop = False (and both have higher TabIndexes (5 and 6).
I have tried in the Form's onLoad event to set the TabIndex to the TextBoxes but I sitll get the same result.
I thought that, maybe because I modify the center textbox during the Resized event, that it automatically gets focused so I tried removing that part and coding around it so it remembered the last item with focus. That also didnt work.
This didn't happen until I put panels onto my form. Is this a known issue or am I missing something totally obvious?
Kasracer
Jan 29th, 2006, 10:26 PM
I attempted to place the center textbox on a panel and now the focus shifts to the first button (the one with a tabindex of 2).
What the heck is going on?
Mike Hildner
Jan 29th, 2006, 10:38 PM
I haven't tried your steps yet, but a couple thoughts.
The center Textbox didn't work correctly with a Dock of Fill (it went under the panel controls) so I placed some simple code in the resize event of the form to manually resize it.
That's probably because it's in the back. Right click and choose bring to front. You probably don't need to write any code.
When you're in the design view of your form or UserControl or whatever, click on View | Tab Order. Notice that there are layers, that is you'll see a tab order of 1.0, 2.0 etc. Does that match the tab behaviour?
Mike
Kasracer
Jan 29th, 2006, 10:56 PM
That's probably because it's in the back. Right click and choose bring to front. You probably don't need to write any code.
Just tried that and it works. Thanks, didn't realize that would cause a dock issue but it makes sense why it would.
When you're in the design view of your form or UserControl or whatever, click on View | Tab Order. Notice that there are layers, that is you'll see a tab order of 1.0, 2.0 etc. Does that match the tab behaviour?
My center text is 0 but my top 1 is 6.1. Ahh. How can I change those?
Mike Hildner
Jan 29th, 2006, 11:24 PM
I wish I knew. I assume it's the order in which they're loaded - maybe the order in the generated code. It doesn't seem like you can tweak that in View | Tab Order.
I usually make sure source control is on and then just go to town deleting, cutting/pasting etc. until I get it right. Urgh. Slowdown for me.
Please let me know if you figure out an easier way to do it. This gets nasty when your forms get complex.
Thanks,
Mike
jmcilhinney
Jan 29th, 2006, 11:55 PM
The problem is that when you add the Panels each of them will have a higher TabIndex than any other controls. That means that any controls on these Panels will be after any controls on the form proper in the Tab order. It doesn't matter what values the TabIndexes are, just what their relative order is. If you have a Button on the form wuth TabIndex 100, then a Panel with TabIndex 101, even if every control on the Panel has a TabIndex lower than 100 they will still be after the other Button because they have to wait until the tab order gets to their container before they are even seen. You can see this represented graphically if you select View -> Tab Order. Basically the tab order cannot jump around between containers. Think of the controls on your form as being in a tree structure and the tab order must follow the tree. It cannot jump from node to node. One entire node must be traversed before the next is visited, e.g.Form -> TextBox1
Button1
Panel1 -> TextBox2
-> TextBox3
Button2
Panel2 -> TextBox4
-> Button3
Mike Hildner
Jan 30th, 2006, 12:00 AM
Ok, but how do you change it? It's easy enough to click View | Tab Order and click around which will set the tab order within panels etc., but how do you change which panel comes first (without cutting and pasting)? Or is that the best way to do it? Seems like View | Tab Order is a little lacking.
Mike
Mike Hildner
Jan 30th, 2006, 12:09 AM
For example, here's a really nasty UI setup. Let's say you're the guy who just inherited this. How do you change the hierarchy?
Thanks,
Mike
jmcilhinney
Jan 30th, 2006, 12:47 AM
You purely and simply click on the controls in the order you want them to be tabbed to. You can click every control, including Panels, which are controls, in the order you want them to follow, or you can just click a single control a number of times until it shows the index you want it to have.
Kasracer
Jan 30th, 2006, 12:52 AM
Thanks for the help.
I found this article on the MSDN: http://msdn.microsoft.com/library/en-us/vbcon/html/vbtsksettingtaborderofform.asp which was really helpful.
What I learned was, if the containers holding your controls have the same tab index, then you just click in the order you want them to work and it works via tab order.
If the containers have different indexes, then when you click, it'll go in order by index.
It makes a lot of sense now and I just fixed my problem.
Thanks guys!
Mike Hildner
Jan 30th, 2006, 12:53 AM
Urgh. Have you tried this? I don't think you understand the issue. Whenever I click on a control that's within some container, the tab order business works just fine. The real issue is resetting which container control is the first tab.
Mike
Mike Hildner
Jan 30th, 2006, 12:55 AM
Urgh. I hate it when my posts get mixed up. Cool find.
Mike
Kasracer
Jan 30th, 2006, 01:00 AM
Urgh. Have you tried this? I don't think you understand the issue. Whenever I click on a control that's within some container, the tab order business works just fine. The real issue is resetting which container control is the first tab.
Mike
The first one would be all zeros.
So your treeview would be the first tab. If you want to reset it, then simply changed the TabIndex property of it. If you have controls on other containers (like a panel), then they automatically have a higher base index, sort to speak.
Let's say you have the following:
http://vbforums.com/attachment.php?attachmentid=44830&stc=1
The first textbox is on a panel. Then the center textbox is by itself with the buttons on its own panel.
Right now, the first textbox will get focus.
If I want to change it so the button first gets focus, I can't change it's TabIndex to be 0 because it's already 0. I have to change the panel's tabindex that contains the button (which is 2, hence the "2.0" and "2.1" over the buttons). If I change that panel to 0, then I can change the order in which things happen and now the button becomes first:
http://vbforums.com/attachment.php?attachmentid=44832&stc=1
I hope I explained that right.
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.