Solved

Losing focus after modal form unloads

Posted on 1998-04-23
3
470 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
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

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…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

785 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