Results 1 to 11 of 11

Thread: [RESOLVED] Bring MessageBox to Front

  1. #1

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Resolved [RESOLVED] Bring MessageBox to Front

    I'm using this code to load data from a file by dragging the file icon on to the app's main form:
    VB Code:
    1. Private Sub Form_OLEDragDrop(Data As DataObject, Effect As Long, Button As Integer, Shift As Integer, X As Single, Y As Single)
    2.  
    3.     If Data.Files.Count > 1 Then
    4.         MsgBox "Only one file can be loaded"
    5.         Exit Sub
    6.     End If
    7.    
    8.     TextFile = Data.Files(1)
    9.     'Now the string variable TextFile has the path to the file name
    10.     'to be read and processed
    11.     '...
    12.     '...some other stuff here to do the job...
    13.  
    14. End Sub
    The inconvenient is the msgbox appears on the background and is not visible if some other window is covering it so the user is not even aware he/she's being prompted for action. How do I bring the msgbox on top?
    Last edited by krtxmrtz; Mar 5th, 2012 at 09:30 AM.
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  2. #2
    PowerPoster Spoo's Avatar
    Join Date
    Nov 2008
    Location
    Right Coast
    Posts
    2,656

    Re: Bring MessageBox to Front

    Krtx

    My understanding is that MsgBox is modal .. ie, always on top.

    This "other" window you are referring to .. is it from your app
    or could it be from some other app?

    Spoo

  3. #3

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Bring MessageBox to Front

    Quote Originally Posted by Spoo View Post
    ...
    This "other" window you are referring to .. is it from your app
    or could it be from some other app?

    Spoo
    It's not from my app, of course. It's an open folder from Windows Explorer.
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  4. #4

  5. #5

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Bring MessageBox to Front

    Quote Originally Posted by RhinoBull View Post
    Try forcing your main form to forground first and then check for file count.
    It worked, thank you!
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  6. #6

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Bring MessageBox to Front

    Quote Originally Posted by krtxmrtz View Post
    It worked, thank you!
    As a matter of fact I was a bit to hasty to answer, the thing is it worked the first time but then it has since failed.

    When I start the app I then open a folder to pick a file to drag. I think the problem is the app form should get the focus back, not (just?) get topmost state.
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  7. #7

  8. #8

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Bring MessageBox to Front

    Quote Originally Posted by RhinoBull View Post
    How did you implement it?
    VB Code:
    1. 'In a module:
    2. Private Declare Function SetWindowPos Lib "user32" (ByVal hWnd As Long, ByVal hWndInsertAfter As Long, _
    3.   ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
    4. Private Const SWP_NOMOVE = &H2
    5. Private Const SWP_NOSIZE = &H1
    6. Private Const SWP_SHOWWINDOW = &H40
    7. Private Const SWP_NOActivate = &H10
    8. Public Enum WindowPos
    9.     vbtopmost = -1&
    10.     vbnottopmost = -2&
    11. End Enum
    12.  
    13. Public Sub SetFormPosition(hWnd As Long, Position As WindowPos)
    14.     Const wFlags As Long = SWP_NOMOVE Or SWP_NOSIZE Or SWP_SHOWWINDOW Or SWP_NOActivate
    15.    
    16.     If Position = vbtopmost Or Position = vbnottopmost Then
    17.         SetWindowPos hWnd, Position, 0, 0, 0, 0, wFlags
    18.     End If
    19.    
    20. End Sub
    21.  
    22. 'In the main form:
    23. Private Sub Form_OLEDragDrop(Data As DataObject, Effect As Long, Button As Integer, Shift As Integer, x As Single, y As Single)
    24.    
    25.     SetFormPosition Me.hWnd, vbtopmost
    26.    
    27.     If Data.Files.Count > 1 Then
    28.         MsgBox "Only one file can be loaded", vbOKOnly, "Notice"
    29.         Exit Sub
    30.     End If
    31.    
    32.     TextFile = Data.Files(1)
    33.     LoadImage
    34.    
    35. End Sub
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  9. #9
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Bring MessageBox to Front

    Try using SetForegroundWindow api without any checking.
    Code:
    Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
    
    '...
    
    Private Sub Form_OLEDragDrop(Data As DataObject, Effect As Long, Button As Integer, Shift As Integer, x As Single, y As Single)
        
        SetForegroundWindow Me.hWnd
        
        If Data.Files.Count > 1 Then
            MsgBox "Only one file can be loaded", vbOKOnly, "Notice"
            Exit Sub
        End If
        
        TextFile = Data.Files(1)
        LoadImage
        
    End Sub

  10. #10

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Bring MessageBox to Front

    Quote Originally Posted by RhinoBull View Post
    Try using SetForegroundWindow api without any checking...
    Works fine and steadily in time.

    Thanks again.
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  11. #11

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