Calling Close() Programmatically

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

When contained in a button click event firied by the UI the close method works.
If I call this method thru code, or call the click event thru code, or
create an event and fire it which calls Close() the method does NOT close
the form.

I am trying to close form in the forms constructor if DB server is down and
show polite message " SQL Server not running, your application is being
closed." ?
 
Herein lies my problem. I have never subsrcibed to the "OK" button click
event in a call to MessageBox.Show(). I have the MessageBox dialog popping
up, Ijust need to get my code into the "OK" button (or any button displayed).
Thanks ccm.
 
ccm said:
I am trying to close form in the forms constructor if DB server is down
and
show polite message " SQL Server not running, your application is being
closed." ?

Throwing an exception is one way to prevent instantiating. Alternatively
you could write a shared (static) factory method that performs instantiation
and returns the form's instance.
 
Herein lies my problem, I have never subscribed to the "OK" button click
event in a call to MessageBox.Show(). I have the pop-up msg, I just need to
get my code into the "OK" click event (or any button type). Because this is
a system dialog and not one I defined, I do not know where capture the event
handler? Thanks ccm.
 
MessageBox.Show() returns a DialogResult where you can get the Information,
which Button was clicked.

But if you only have one Button why don't you do it in this way:

=============
// Show Messagebox
MessageBox.Show("SQL Server not running, your application is being closed",
"SQL Server Error");

// Close Application
Application.Exit();
=============

The MessageBox.Show() Method shows an so called "modal" Dialog. Because of
that the Execution of the calling Method will hold. When you close the
MessageBox by clicking OK then the Execution will go on and in the above
example the Method Application.Exit() will be called.

When you design your own Forms, then you can show them either by calling
Show() (non-modal) or by calling ShowDialog() (modal).

Regards,
Christian
 
The MessageBox.Show method is modal. That is to say that it will not return
until the dialog had been dismissed. You can just do this...

MessageBox.Show("The Datatbase is not working");
Application.Exit();

For more detail, for example if you've put retry-cancel buttons on the
dialog you can read the dialog result like so:

DialogResult dr= MessageBox.Show("The Datatbase is not working");
//do something with the result here...

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
Yes exactly, that is precisely what I am doing(see code below). Quick aside,
I only need to capture the "OK" click event. Once I do this I could easily
handle any of the other button types in the MessageBox. But you are right why
bother.

The code below does not close the window. It may be that all method calls in
the public contructor are executing before the Exit or close call is executed

/***************
public myForm() //ctor
{
InitializeComponent();
VerifySQLServer(); //note this will throw Exception, server is off
line for testing
Read SQLServer(); //here is where 2nd Exception is thrown, AFTER I
called Exit()
}
private void VerifySQLServer()
{
try
{
conn = new SqlConnection(connString);
conn.Open();
}
catch(SqlException exc)
{
string error = exc.Message;
MessageBox.Show("SQL Server Unavailable, Please have administrator
verify SQL Database Server is running. \nERROR: " + error);

Application.Exit();
this.Close();
}

Since the above does not work an alternative solution is to capture the "OK"
click event from the system message dialog (not button type). In the handler
simply issue a call to Close(). Also, I can run a timer and successfully
process a close call that way. However, I really would like to handle the
"OK" click event. Thanks Again - ccm
 
Back
Top