Solved

Losing focus after modal form unloads

Posted on 1998-04-23
3
468 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
  • 2
3 Comments
 
LVL 9

Accepted Solution

by:
Dalin earned 100 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now