Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Losing focus after modal form unloads

Posted on 1998-04-23
3
Medium Priority
?
492 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 400 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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 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…
Suggested Courses

885 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