Main Form loses focus

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.

Problem:

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.

Solution:

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.

    Copy(source, destination);
this.Focus();

Problem solved.

Advertisements

One thought on “Main Form loses focus

  1. andry says:

    i3n2ut comment3 ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: