Solved

Losing focus after modal form unloads

Posted on 1998-04-23
3
477 Views
Last Modified: 2008-03-17
We're using VB5 and I'm having a problem with my application losing focus after we unload modal forms.

We have a MDI application which has many MDI-child forms.  On some of these child forms, we occasionally need to pop up a modal dialog box.  If the user presses Cancel on the modal dialog box, we put up a message box to confirm that they want to quit.  If they say yes, the modal form unloads.  At this point, the original mdi-child form should get the focus.  However, if you were running another app in the background (like Notepad), it jumps up over top of the application and has the focus.  If we comment out the call to the message box, this problem doesn't happen.

I can get around the problem by manually doing a .SetFocus call after the "ModalForm.Show 1" call, but the display looks funcky.  Notepad will still flash to the foreground and then our app will come back into focus.  I'd prefer a better solution.

This app was ported from VB3 and we never had this problem.  It only seems to happen in VB5 if the modal form is unloaded immediately after a MsgBox is displayed.

It looks like this is a VB5 bug.  By the way, I'm running SP3 of VB5 and SP3 of NT 4.0.

Thanks.
0
Comment
Question by:jsullivan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 9

Accepted Solution

by:
Dalin earned 100 total points
ID: 1428313
jsullivan,
here are two possible workarounds for this problem:

           Set the focus to the MDI child form after showing the non-MDI child form with the SetFocus method.

           -or-
           Use the SetForegroundWindow API in the unload event of the modal form. The MORE INFORMATION section below shows you how to create
           a project that demonstrates this problem. If you want this sample project to include the workaround, add the following code to Form2:

                 Option Explicit
                 Private Declare Function SetForegroundWindow Lib "user32" _

           (ByVal hwnd As Long) As Long Private Sub Form_Unload(Cancel As Integer) SetForegroundWindow MDIForm1.hwnd End Sub

Regards
Dalin
0
 

Author Comment

by:jsullivan
ID: 1428314
Dalin,

Your first proposed solution was what I described in my question.  It works, but you still see other applications blink to the foreground, which is unacceptable.

Fortunately, the second solutions seems to work.  I still bet that this is a VB5 bug and I hope that it is fixed in the next patch.

Thanks for the tip.

0
 
LVL 9

Expert Comment

by:Dalin
ID: 1428315
jsullivan,
I just copied it from my database that I stored some time ago.
Yes, you are right. It is a reported Bug.
Good luck in your project
Dalin

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question