tags:

views:

1351

answers:

5

I have a UserControl that consists of three TextBoxes. On a form I can have one or more or my UserControl. I want to implement my own tab behavior so if the user presses Tab in the second TextBox I should only move to the third TextBox if the the second TextBox has anything entered. If nothing is entered in the second TextBox the next control of the form should get focus as per the normal tab behavior. If the user hasn't entered anything in the first or second TextBox and the presses tab there is this special case where a control on the form should be skipped.

By using the ProcessDialogKey I have managed to get it work kind of ok but I still have one problem. My question is if there is a way to detect how a WinForms control got focus since I would also like to know if the my UserControl got focus from a Tab or Shift-Tab and then do my weird stuff but if the user clicks the control I don't want to do anything special.

+3  A: 

As a general rule, I would say overriding the standard behavior of the TAB key would be a bad idea. Maybe you can do something like disabling the 3rd text box until a valid entry is made in the 2nd text box.

Now, having said this, I've also broken this rule at the request of the customer. We made the enter key function like the tab key, where the enter key would save the value in a text field, and advance the cursor to the next field.

Rob Thomas
+1  A: 

I don't think there's a built-in way that you could do it. All of the WinForms focus events (GotFocus,LostFocus,Enter,Leave) are called with empty EventArgs parameters, which will not give you any additional information.

Personally, I would disable the third textbox, as Rob Thomas said. If you're determined to do this, though, it wouldn't be difficult to set up a manual (read: hackish) solution. Once the tab key is pressed (if the focus is on the second textbox), set a variable inside your form. If the next object focused is then the third textbox, then you know exactly how it happened.

DannySmurf
A: 

I agree with DannySmurf. Messing with the tab order might give you hell later on if the requirements for the application change.

Another thing that you could do is to implement some kind of wizard for the user to go through.

Patrik
A: 

The reason for this odd tab behavior is all about speed in the input process. It was really good to get some input, I hadn't thought about disabling a textbox but that could actually work. But using the Enter key to accept the input hadn't even crossed my mind. That will work so much better. The user can enter the numbers and then press enter to accept the input and the next possible textbox will be the active one. It's like having the cake and eating it too, The speed factor is there since when using the enter key no unnecessary tabing must be done to get to the correct field and using the enter key next to the numeric keyboard makes it really smooth.

Thanks for the input!

Robert Höglund
A: 

Better than disabling controls, try monkeying around with TabStop - if this is false, the control will be simply skipped when tabbing.

I'd also suggest that the Changed event of the TextBox is the place to be updating TabStop on the other controls.

I've done something similar to this with a login control, where users could enter either a username or an email address (in separate fields), plus their password, and tabStop is what I used to get the job done.

Bevan