Solved

Losing focus after modal form unloads

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
add text to end of existing text in file 16 69
Macro: print each sheet to pdf using ExportAsFixedFormat 11 40
message box in access 4 41
MS Date Picker 64 bit 32 bit issue 12 49
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…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

920 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

17 Experts available now in Live!

Get 1:1 Help Now