Alternative to MsgBox?

I like how MsgBox stops all further processing until it's clicked but I don't like how it pops that little msgbox in the middle of the screen, which I then have bring to the front if there is a window covering it. Is there a way to do something like a msgbox but keep it on my Form (which is always in view) so I can go ahead and click a button on the form instead of having to bring the little msgbox window on top of windows that may be covering it.

I use the msgbox to inform me that a certain situation has happened, which I manually fix and when I'm done I click the MsgBox to continue processing. It'd be nice to have this button right on the form.

Thanks
JohnDoeSrAsked:
Who is Participating?
 
BrianGEFF719Connect With a Mentor Commented:
Here is a simple, very simple, example of how to create a non-blocking messagebox by using another thread:


Option Explicit
Private Declare Function CreateThread Lib "kernel32" (ByVal lpThreadAttributes As Any, ByVal dwStackSize As Long, ByVal lpStartAddress As Long, lpParameter As Any, ByVal dwCreationFlags As Long, lpThreadID As Long) As Long
Private Declare Function TerminateThread Lib "kernel32" (ByVal hThread As Long, ByVal dwExitCode As Long) As Long
Private Declare Function MessageBox Lib "user32" Alias "MessageBoxA" (ByVal hwnd As Long, ByVal lpText As String, ByVal lpCaption As String, ByVal wType As Long) As Long
Private lThreadHandle As Long
Private lEventHandle As Long
Private mRes As VbMsgBoxResult
Private mTxt As String
Private mCap As String
Private mTyp As VbMsgBoxStyle

Public Function nonBlockingMsgBox(strTxt As String, strCaption As String, mType As VbMsgBoxStyle)
    Dim lpThreadID As Long
    mTxt = strTxt
    mCap = strCaption
    mTyp = mType
    lThreadHandle = CreateThread(ByVal 0&, ByVal 0&, AddressOf msgbox_fnc, ByVal 0&, 0, lpThreadID)
End Function

Private Function msgbox_fnc()
mRes = MessageBox(0, mTxt, mCap, mTyp)
If lThreadHandle Then TerminateThread lThreadHandle, 0
End Function





To do this, in your main form you woul do nonBlockingMsgBox "Test Text", "My Caption", 0
0
 
BrianGEFF719Commented:
I guess you could just create a new label & text box on a form.

Call the new label 'mErrLabel' and the new Button 'bButtonErr'

then you could do something like:

private sub raise_err(strTxt as string, strButton as string)
mErrLabel.caption = strTxt
bButtonErr.caption = strButton
mErrLabel.visible = true
bButtonErr.visible = true
end sub


and in your button click event, just hide the error label and button again.



Brian
0
 
Dany BalianCTOCommented:
add a frame to your form...
and put all your objects in that form (we'll call this the mainframe)

add another small frame, with a label in it and a button. (we'll call this popframe)
now instead of doing the msgbox... u do the following:

mainframe.enabled=false 'if you disable the frame all objects in that frame will become not clickable... or accessible
popframe.visible=true  'this will show the popframe (like a msgbox)

p.s. note that you popframe should not be in the mainframe orelse it will not be accessible as well...

hope this helps..

cheers

dan

0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
Naveen SwamyYash Infinite Solutions Private LimitedCommented:
yeah using/creating a msgbox with advaced option is good to do but bringing the msgbox on top of the applications could be achieved with the help of vbSystemModal
or
you could design an applicable form and show it as modal form, this will act like a messagebox
0
 
debtdudeCommented:
Private Function NewMsgBox(sMsg As String, lButtons As VbMsgBoxStyle, sTitle As String) As VbMsgBoxResult
Dim RC As VbMsgBoxResult

   Call SetWindowPos(Me.hwnd, HWND_NOTOPMOST, 0, 0, 0, 0, _
                     SWP_NOSIZE Or SWP_NOMOVE)
                                               
   RC = MsgBox(sMsg, lButtons, sTitle)
   
   Call SetWindowPos(Me.hwnd, HWND_TOPMOST, 0, 0, 0, 0, _
                     SWP_NOSIZE Or SWP_NOMOVE)
   
   NewMsgBox = RC
   
End Function
0
 
BrianGEFF719Commented:
debtdude: Your code has a few logical errors.

I think what you ment to do was bring the form to the front _while_ the message box was displayed.

Your code will not bring the window to the front until the message box is closed (execution was halted while awaiting response of message box).

The only way your code would work is if the MessageBox was called from another thread.
0
 
StimphyCommented:
Look at this example

http://vb.mvps.org/samples/project.asp?id=MovedMsg

Scroll to the bottom of the page to see the dl link
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.