469,323 Members | 1,599 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,323 developers. It's quick & easy.

How to programmatically close a messagebox?

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?

Thanks.
Jun 26 '07 #1
6 20882
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.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Frank Rizzo" <no**@none.comwrote in message
news:u9**************@TK2MSFTNGP03.phx.gbl...
>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?

Thanks.
Jun 26 '07 #2
"Frank Rizzo" <no**@none.comwrote in message
news:u9**************@TK2MSFTNGP03.phx.gbl...
>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...
--
http://www.markrae.net

Jun 26 '07 #3
Mark Rae wrote:
"Frank Rizzo" <no**@none.comwrote in message
news:u9**************@TK2MSFTNGP03.phx.gbl...
>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...
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.
Jun 27 '07 #4
On Tue, 26 Jun 2007 17:11:02 -0700, Frank Rizzo <no**@none.comwrote:
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
Jun 27 '07 #5
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/subscripti...ult.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/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
Jul 2 '07 #6

""Jeffrey Tan[MSFT]"" <je***@online.microsoft.comschrieb im Newsbeitrag
news:jf**************@TK2MSFTNGHUB02.phx.gbl...
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/subscripti...ult.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/subscripti...t/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/miscct...IndirectCS.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ü
-----------------------
"This reply is provided as is, without warranty express or implied."




Jul 2 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.

By using this site, you agree to our Privacy Policy and Terms of Use.