Close window after MessageBox

P

PawelR

Hello group,
In my apps I connection to SQL database.
try
{
//connect to db
}
catch
{
MessageBox("Error Connect. Close Apps?","Sql
Error",MessageBoxButtons.OKCancel,MessageBoxIcon.Error);

}
When db is connection error apps show messagebox with button OK and Cancel.
My question: How close this application after OK, and continuue apps after
Cancel?


catch
{
MessageBox("Error Connect","Sql
Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
Close();
}
Not work.


Thx PawelR
 
S

Stefan

hi Pawel
use the dialogresult object
DialogResult dlr = (Messagebox.Show("blablabla",
MessageBoxButtons.OkCancel)
if (dlr == DialogResult.Ok)
{
close();
}
else
{
do...;
}
 
N

Nicholas Paldino [.NET/C# MVP]

PawelR,

I think that what you want is the static Quit method on the Application
class. It should shut down your application, and as a result, all of the
windows that go with it.

Hope this helps.
 
M

Morten Wennevik

this.Close();

should close the current thread(program).
However, if you connect to the database as a part of a startup procedure
Close() will be ignored.

A possible solution to this is to throw a new exception after showing the
messagebox, and use a try/catch around Application.Run();

You put the messagebox in an if statement and compare it with
DialogResult.OK to check if OK was clicked.

[STAThread]
static void Main()
{
try
{
Application.Run(new Form1());
}
catch
{
}
}

(...)

try
{
//connect to db
}
catch
{
if(MessageBox("Error Connect. Close Apps?", "Sql Error",
MessageBoxButtons.OKCancel,MessageBoxIcon.Error) == DialogResult.OK)
{
throw new Exception();
}
}
 

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