-
Mar 5th, 2012, 06:58 AM
#1
[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:
Private Sub Form_OLEDragDrop(Data As DataObject, Effect As Long, Button As Integer, Shift As Integer, X As Single, Y As Single)
If Data.Files.Count > 1 Then
MsgBox "Only one file can be loaded"
Exit Sub
End If
TextFile = Data.Files(1)
'Now the string variable TextFile has the path to the file name
'to be read and processed
'...
'...some other stuff here to do the job...
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)
-
Mar 5th, 2012, 08:21 AM
#2
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
-
Mar 5th, 2012, 08:34 AM
#3
Re: Bring MessageBox to Front
Originally Posted by Spoo
...
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)
-
Mar 5th, 2012, 08:43 AM
#4
Re: Bring MessageBox to Front
Try forcing your main form to forground first and then check for file count.
-
Mar 5th, 2012, 09:14 AM
#5
Re: Bring MessageBox to Front
Originally Posted by RhinoBull
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)
-
Mar 5th, 2012, 09:35 AM
#6
Re: Bring MessageBox to Front
Originally Posted by krtxmrtz
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)
-
Mar 5th, 2012, 09:38 AM
#7
Re: Bring MessageBox to Front
How did you implement it?
-
Mar 5th, 2012, 09:46 AM
#8
Re: Bring MessageBox to Front
Originally Posted by RhinoBull
How did you implement it?
VB Code:
'In a module:
Private Declare Function SetWindowPos Lib "user32" (ByVal hWnd As Long, ByVal hWndInsertAfter As Long, _
ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
Private Const SWP_NOMOVE = &H2
Private Const SWP_NOSIZE = &H1
Private Const SWP_SHOWWINDOW = &H40
Private Const SWP_NOActivate = &H10
Public Enum WindowPos
vbtopmost = -1&
vbnottopmost = -2&
End Enum
Public Sub SetFormPosition(hWnd As Long, Position As WindowPos)
Const wFlags As Long = SWP_NOMOVE Or SWP_NOSIZE Or SWP_SHOWWINDOW Or SWP_NOActivate
If Position = vbtopmost Or Position = vbnottopmost Then
SetWindowPos hWnd, Position, 0, 0, 0, 0, wFlags
End If
End Sub
'In the main form:
Private Sub Form_OLEDragDrop(Data As DataObject, Effect As Long, Button As Integer, Shift As Integer, x As Single, y As Single)
SetFormPosition Me.hWnd, vbtopmost
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
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)
-
Mar 5th, 2012, 10:04 AM
#9
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
-
Mar 5th, 2012, 10:20 AM
#10
Re: Bring MessageBox to Front
Originally Posted by RhinoBull
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)
-
Mar 5th, 2012, 01:37 PM
#11
Re: [RESOLVED] Bring MessageBox to Front
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|