Unable to restore focus to main window after calling dialog boxes

S

Steve Sargent

Hi:

I'm having problems restoring the focus to my main window after
calling a couple of dialog boxes. Sometimes the focus is restored,
other times not.

In my code, when setting up an account, the user clicks the button and
the event handler for the button pops up. The event handler then
calls thread to handle the setting up of accounts. This thread then
calls 3 dialog boxes to gather information. Intermixed ars 2 message
boxes with simple ok buttons.

At the end of the thread (the one created from within the event
handler), in the finally block, I have code to restore the focus. It
checks as to whether invocation is required, and if so, invoke a
function to set focus and refresh this form. (if not needed, I call
this function directly).

The code I'm using to reset the display is

this.Focus();
this.Refresh();

Am I missing something? I've checked to make sure that none of my
dialog boxes have TopMost set to true. Are there other functions I
need to be working with to get my original window to resurface after
the account setup takes place?

This seems to work for the most part. However, sometimes it doesn't
do anything at all. I've traced the code and stepped through the
callsf as they call these to calls, and sometimes they work, sometimes
they don't.

Any help with this is greatly appreciated.

Steve
 
G

Guest

Well, first of all, you shouldn't have to be doing anything to reset the
focus to the main window.

Are you passing the main form as the owner parameter of the other forms?

It may have to do with the threading. Do you really need to be doing this
stuff in separate threads? Your UI should generally all be handled in a
single thread with non-UI processing taking place in separate threads, if
needed.

Pete
 
S

Steve Sargent

Thank you for the reply.

Yes, it does need to be in a thread. And I know about the threading
issue (I wouldn't have put in the invocation calls if I didn't), but
not everything can run from the gui thread. The routines called are
not always used by the application (same routines get called in the
Installer class's Install and Uninstall methods).

Thanks for the advice.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top