How to programmatically close a messagebox?

N

Nicholas Paldino [.NET/C# MVP]

Frank,

Well, there aren't any managed ways to do this. You will have to use
the FindWindow and EndDialog api's just as you would in unmanaged code.

If you want to make sure that the dialog belongs to your app, the only
way I can think of is to get the handle of the window, and then call the
GetWindowThreadProcessId API function through the P/Invoke layer. It will
give you the thread id and the process id that the window belongs to. With
that, you should be able to make a match between the messagebox dialog and
your app.
 
M

Mark Rae

I am trying to programmatically close a messagebox. I don't see any
obvious managed choices. Back in the day, I remember using a combination
of FindWindow and EndDialog apis(
http://msdn2.microsoft.com/en-us/library/ms645472.aspx ), but how can I
ensure that the dialog that I am destroying actually belongs to my
application?

In addition to Nicholas' response, I'm slightly curious about this...

Is the MessageBox in question not being generated by an application which
you are writing...? If so, then surely you have control over the code which
generates the MessageBox in the first place...

So, if you don't need it, don't show it...
 
F

Frank Rizzo

Mark said:
In addition to Nicholas' response, I'm slightly curious about this...

Is the MessageBox in question not being generated by an application
which you are writing...? If so, then surely you have control over the
code which generates the MessageBox in the first place...

So, if you don't need it, don't show it...

Basically the situation is this. The application is supposed to log
people off after an hour of inactivity. If they performed an operation
that threw up a dialog box (such as a validation of some sort) and then
the user went to lunch, at some point I need to get rid of the message
box, close some other windows and bring the app to the login screen.
 
P

Peter Duniho

Basically the situation is this. The application is supposed to log
people off after an hour of inactivity. If they performed an operation
that threw up a dialog box (such as a validation of some sort) and then
the user went to lunch, at some point I need to get rid of the message
box, close some other windows and bring the app to the login screen.

So, taking as an assumption that your code IS the code that's calling the
MessageBox.Show() method, I would recommend that the answer is to not use
MessageBox. Instead, just use your own custom form, calling ShowDialog()
on it to do basically the same thing as the MessageBox class. Then, you
have the instance of the form itself, and you can call Close() on that
instance to close it.

I assume, of course, that whatever is being displayed is known to be
cancellable without any serious harm (that is, it's not a yes/no
MessageBox that says "should I keep all of the work that you've done over
the last 48 hours?")

Pete
 
J

Jeffrey Tan[MSFT]

Hi Frank ,

How about your issue now? Normally, I agree with Peter that you'd better
create a customized messagebox to show to the user so that you can control
its life time through the reference easily.

If you still need any help, please feel free to feedback, thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
K

Kerem Gümrükcü

"Jeffrey Tan[MSFT]" said:
Hi Frank ,

How about your issue now? Normally, I agree with Peter that you'd better
create a customized messagebox to show to the user so that you can control
its life time through the reference easily.

If you still need any help, please feel free to feedback, thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no
rights.

Hi Frank,

there is a way to do this, but not managed. You can set a CBT Hook
right before you call the MsgBox and then look for the MsgBox in the
Callback and manipulate it from there the way you want it, e.g. Add
Buttons, Icons, close it, etc,...

I have a lot of Visual Basic 6 Code but i think this is more handy
for you: http://www.codeproject.com/cs/miscctrl/MessageBoxIndirectCS.asp
Its in C# and a part of the code and article shows exactly what you want.


Beste Grüsse / Best regards / Votre bien devoue

Kerem Gümrükcü
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top