Results 1 to 7 of 7

Thread: [RESOLVED] MsgBox focus question

  1. #1

    Thread Starter
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,969

    Resolved [RESOLVED] MsgBox focus question

    I have a project that the user works two windows at once split so they are visible on the same screen.. Form1 is the main VBP project and works in junction with an OCX that controls a third party viewer. When the user clicks save on Form1 it has the focus. Then it calls the OCX which displays a msgbox which is under form1. The reverse happens when the user makes selections on the viewer. It has focus and when control passes back to form1 and displays a message it is under the viewer. How can I make the message box stay in front of both forms? I tried me.setfocus in the OCX and it doesn’t seem to support it. Someone pointed me at an API that is called SetWindowPos but it doesn’t seem to work. “Using MsgBox "test", vbMsgBoxSetForeground” seems to set it in front but I need to check for a yes no response.

    Any suggestions?

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: MsgBox focus question

    Is your form1 set as the topmost form perhaps? Otherwise a msgbox should display above its parent form.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: MsgBox focus question

    It depends on message box instance. Here is a Unicode aware API version of the standard messagebox:
    Code:
    Option Explicit
    
    Private Declare Function MessageBoxIndirectW Lib "user32" (lpMsgBoxParams As MsgBoxParams) As Long
    
    Private Const MB_USERICON = &H80&
    
    Private Type MsgBoxParams
        cbSize As Long
        hWndOwner As Long
        hInstance As Long
        lpszText As Long
        lpszCaption As Long
        dwStyle As Long
        lpszIcon As Long
        dwContextHelpId As Long
        lpfnMsgBoxCallback As Long
        dwLanguageId As Long
    End Type
    
    ' note: I didn't bother to go ahead and start hacking with HelpFile and Context
    Public Function MsgBox(ByVal Prompt As String, Optional Buttons As VbMsgBoxStyle = vbOKOnly, Optional ByVal Title As String, Optional ResourceIcon As String, Optional hWndOwner As Long) As VbMsgBoxResult
        Dim udtMsgBox As MsgBoxParams
        ' if no owner is specified, try to use the active form
        If hWndOwner = 0 Then If Not Screen.ActiveForm Is Nothing Then hWndOwner = Screen.ActiveForm.hWnd
        With udtMsgBox
            .cbSize = Len(udtMsgBox)
            ' important to set owner to get behavior similar to the native MsgBox
            .hWndOwner = hWndOwner
            .hInstance = App.hInstance
            ' set the message
            .lpszText = StrPtr(Prompt)
            ' if no title is given, use the application title like the native MsgBox
            If LenB(Title) = 0 Then Title = App.Title
            .lpszCaption = StrPtr(Title)
            ' thought this would be a nice feature addition
            If LenB(ResourceIcon) = 0& Then
                .dwStyle = Buttons
            Else
                .dwStyle = (Buttons Or MB_USERICON) And Not (&H70&)
                .lpszIcon = StrPtr(ResourceIcon)
            End If
        End With
        ' show the message box
        MsgBox = MessageBoxIndirectW(udtMsgBox)
    End Function
    The things of interest for you are hInstance and hWndOwner. As long as you set the the same owner and instance in each call, you get the same kind of behavior.

  4. #4

    Thread Starter
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,969

    Re: MsgBox focus question

    Quote Originally Posted by RobDog888
    Is your form1 set as the topmost form perhaps? Otherwise a msgbox should display above its parent form.
    I probably should have been clearer. Form1 is the project. The first of the two forms the user is working with is a dll. The dll calls the ocx. Those two are what's sharing the screen and hiding each others message boxes.

    So...the main screen is up, the user calls the dll display and the form1 goes to the tool bar. Clicking on the dll calls the ocx display which is now side by side with the dll display Working between the two is where they are blocking each others messages.

  5. #5

    Thread Starter
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,969

    Re: MsgBox focus question

    This seems to work fine:

    MsgBox "text", vbInformation + vbMsgBoxSetForeground, "text"

  6. #6
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: MsgBox focus question

    Quote Originally Posted by TysonLPrice
    This seems to work fine:

    MsgBox "text", vbInformation + vbMsgBoxSetForeground, "text"
    But, that doesn't seem to satisfy this requirement.
    Quote Originally Posted by TysonLPrice
    I need to check for a yes no response.
    Don't you need to add a vbYesNo in there?

  7. #7

    Thread Starter
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,969

    Re: MsgBox focus question

    Quote Originally Posted by Hack
    But, that doesn't seem to satisfy this requirement.Don't you need to add a vbYesNo in there?
    Yes...What I posted was from the form load of a simple project to test and to show what works for keeping the msgbox on top in case someone searches in the future. This is what I actually used:

    Code:
    Select Case MsgBox("Do you want to index the remaining pages after the split?" & vbCrLf & _
                               "Clicking 'No' will delete the remaining pages", vbYesNoCancel + vbMsgBoxSetForeground, "Confirm Keep Pages.")
            Case vbYes
                bKeepRemainingPages = True
            Case vbNo
                bKeepRemainingPages = False
            Case vbCancel
                Exit Function
            End Select
    Last edited by TysonLPrice; Jul 2nd, 2008 at 04:50 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width