1
votes

Using c# winforms vs2008

I'm using a TabControl on a form. Each tab has a different set of input fields. Some of those fields has to be completed. Such as Username. Multiple users will use the same PC, so a the username must remain static. I have a leave event on the require fields which triggers when the textbox loses focus to check if a value was added. In the case of Username, a popup would then presents possible values. That works pretty awesome to ensure accuracy and speed. To problem comes in when the user clicks on another tab, the textbox leave event triggers. How can I prevent the leave_event code from running if the focus changes to a control not on the current Tab?

I have tried different event handlers, but the leave event seem to occur first. I tried textbox validating event handler, but also no success. I tried adding a If-statement in front of the code to check the tab number, or tabcontrol state - no joy - as before anything else changes, the leave event fires.

The only remaining thing I can think of is to loop through all the controls on the tab to check if they have focus, but that just feels messy.

Any other suggestions?

1
have you tried using the TextChanged event instead of the leave event?Robson
The Validating event should be used, not Leave. Why it "doesn't work" is entirely unobvious from the question. Just don't do it this way at all, trapping the user like that is too unfriendly, use the TabControl.Selecting event instead. Validate the required fields, set e.Cancel = true if you are unhappy so the selected tab doesn't change.Hans Passant
See here for an example of how to prevent the user to leave a dirty tab.TaW
Robson: TextChanged wont work, as that will execute on change. I want a action when the text didn't change.vanman
Hans: Why is validating better? I said it didn't work as that event will still get precedence before the tabcontrol gets focus. So stopping the event from firing wont work as it "at the snap shot in time" still dont know I clicked the tab. I'm not really trapping the user. Leaving the textbox without text only gives a popup for possible options. They can cancel the popup and move on. I'm doing it that way because it makes it quicker to only use the keyboard. So it is more like a "if you dont want to type the username, press tab and get options" type of thing.vanman

1 Answers

-1
votes

I have found a partial solution. Using a Mouse_Enter and _Leave event on the tab, I set a flag to determine whether the mouse was clicked in the form or outside. If the flag is set to false, the leave event dont run. This will not solve short cut key presses though. But that I can handle later.