<p>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. </p>

<p>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. </p>

[{'answer_id': 13358, 'author': 'Rob Thomas', 'author_id': 803, 'author_profile': 'https://Stackoverflow.com/users/803', 'pm_score': 3, 'selected': True, 'text': "<p>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.</p>\n\n<p><em>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.</em></p>\n"}, {'answer_id': 13376, 'author': 'TheSmurf', 'author_id': 1975282, 'author_profile': 'https://Stackoverflow.com/users/1975282', 'pm_score': 1, 'selected': False, 'text': "<p>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.</p>\n\n<p>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.</p>\n"}, {'answer_id': 13382, 'author': 'Patrik Svensson', 'author_id': 936, 'author_profile': 'https://Stackoverflow.com/users/936', 'pm_score': 0, 'selected': False, 'text': '<p>I agree with DannySmurf. Messing with the tab order might give you hell later on if the requirements for the application change.</p>\n\n<p>Another thing that you could do is to implement some kind of wizard for the user to go through.</p>\n'}, {'answer_id': 13541, 'author': 'Robert Höglund', 'author_id': 143, 'author_profile': 'https://Stackoverflow.com/users/143', 'pm_score': 1, 'selected': False, 'text': "<p>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. </p>\n\n<p>Thanks for the input!</p>\n"}, {'answer_id': 227381, 'author': 'Bevan', 'author_id': 30280, 'author_profile': 'https://Stackoverflow.com/users/30280', 'pm_score': 0, 'selected': False, 'text': "<p>Better than disabling controls, try monkeying around with TabStop - if this is false, the control will be simply skipped when tabbing.</p>\n\n<p>I'd also suggest that the Changed event of the TextBox is the place to be updating TabStop on the other controls.</p>\n\n<p>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.</p>\n"}]
['https://Stackoverflow.com/questions/13353', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/143/']