Apologies if this post comes across as naive. I recently put my foot into the Windows Forms world and I have been learning new things. This might be a problem which all GUI programmers have encountered on day one and also know the solution to it. This post is to those souls like me who make a late entry into this magical world and seem to get lost, err… lose focus.
We have a main form called MainForm. Whenever the user clicks a button on this MainForm, a method Copy in a class HelperClass is called. This method Copy uses the kernel call FileSystem.CopyFile to carry out the copying. One reason to use the kernel call is you need not re-write the progress bar and cancel features, just reuse the features provided by the kernel call. This will display the standard Windows file copying dialog with a cancel button.
The problem is, whenever you click Cancel on the file copying dialog, the MainForm loses focus.
Since we are using the file copying dialog provided by the kernel, we have no access to the dialog directly. Hence, we cannot set the MainForm as the owner of the file copying dialog.
The solution to this problem is, in MainForm, right after we make a call to Copy method, set the focus back to MainForm.