I'm a huge fan of utilizing Alt-S/Ctrl-S to save and move on in my applications. Saving tends to be the most common operation and that two finger salute is so universal it comes natural <g>...

 

But there's is one problem that I just cannot stand and it happens in just about every environment.

 

Problem: You’re in edit mode on a form, and you now are ready to exit so you hot-key Ctrl-S or Alt-S to save and… you end up loosing the changes in the last text/editbox you were editing. Basically Windows doesn’t recognize your hotkey action as a ‘moving off’ the control so the control’s internal value property (.Text in .NET) is not updated. So your code probably saves happily away not realizing that the value actually isn’t saved.

 

This is often insidious because you see all the rest of the data saved, just that one value isn't.

 

The workaround is easy enough but ugly. I usually have a method on the form that does something like this:

 

private void FixExitFocus()

{

      if (this.ActiveControl != null)

      {

            // *** Force focus to 'save'

            Control ctrl = this.ActiveControl;

            this.lblSitename.Focus();

            Application.DoEvents();

            ctrl.Focus();

                 

      }

}

 

I then call this method, for anything that relies on the values on the form to perform an action. Usually that's a save, but also something that requires the live data from the form right now before it's even saved. This code basically shifts focus away and then back to the control which forces writing the data to the .Text property and the underlying databinding mechanism. In FoxPro you used to be able to cheat this by setting focus to the same control which would also work, which makes the job a lot easier, since you don’t have to go looking out for another control to focus on.

 

Maybe I’m not thinking about this right but short of walking the control collection and finding some control to set focus to then off I don’t see a generic solution here…

 

You also see this behavior in some major applications - I guess I automatically am in the habit of tabbing off everything before saving or explictly clicking on a button.

 

My current scenario specifically is a WinForm with Sandbar on it and using menu items that are hotkeyed. But the same behavior applies to the stock Toolbars and even button hotkeys.