On Windows mobile 6.1 prof.
I have a messagebox with a yes/no button on it. When I click 'No' option in the messagebox, my whole application shutsdown, how can I simply close the messagebox?
string message = "Application will perform a data download agree?";
string caption = "";
MessageBoxButtons buttons = MessageBoxButtons.YesNo;
DialogResult result = MessageBox.Show(message, caption, buttons, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
if (result == DialogResult.Yes)
{
navigateForward(WEB_PAGE_NAVIGATE);
}
else
{
this.Close();
}
You do not need to close the message box. It's a DialogWindow and will close itself when you click any of the options:
DialogResult result = MessageBox.Show();
if (result == DialogResult.Yes)
{
navigateForward(WEB_PAGE_NAVIGATE);
}
else
{
// No need to do anything here as the MessageBox is closed automatically.
}
The reason your whole application shuts down is because this relates to the class you're currently in. I'm guessing that class is your main Form and when your main form is closed, the application shuts down.
Related
I have a C# GUI application. When the user clicks on the red 'X' (for closing the app) I want to show a message and ask if he really wants to close it.
I found a solution:
DialogResult dialog = MessageBox.Show("Do you really want to close the program?", "SomeTitle", MessageBoxButtons.YesNo);
if (dialog == DialogResult.Yes)
{
Application.Exit();
}else if (dialog == DialogResult.No)
{
//don't do anything
}
When the user clicks 'yes', the application should terminate completely. (Is Application.Exit() correct for this purpose?)
When the user clicks 'no', the DialogResult/MessageBox should close, but the application should stay opened. However, it closes!!
How can I avoid this?
BTW: I use Visual Studio 2010 and Winforms.
Use the FormClosing event from the Form, and the FormClosingEventArgs to cancel the process.
example:
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
DialogResult dialog = dialog = MessageBox.Show("Do you really want to close the program?", "SomeTitle", MessageBoxButtons.YesNo);
if (dialog == DialogResult.No)
{
e.Cancel = true;
}
}
Use the form's FormClosing event of your program window. You can then set e.Cancel to true if the user clicks no:
this.FormClosing += (s, e) => {
DialogResult dialog = dialog = MessageBox.Show("Really close?", "SomeTitle",
MessageBoxButtons.YesNo);
if (dialog == DialogResult.No)
{
e.Cancel = true;
}
};
I guess you are using FormClosed. Are you? Then it's too late.
Try this
this.FormClosing += new FormClosingEventHandler(delegate(object sender, FormClosingEventArgs e)
{
if (MessageBox.Show("Do you really want to exit this application?", MessageBoxButtons:=MessageBoxButtons.YesNo) == DialogResult.No)
{
e.Cancel = true;
}
});
Refer to Mudu's answer.
Basically unless you specify additional parameters to MessageBox.Show() you cannot get any result other than DialogResult.Ok from a default MessageBox.
The code you posted (minus your little typo of dialog = dialog =) works exactly as expected to me.
Also: Application.Exit() IS the correct way of closing an application :)
I made an exit button as menu item with confirmation message
and below code do the job,.. but when I am trying to handle the X button I am getting the confirmation message twice I tried to comment the message on the exit button on the menu item but still getting the message twice when I click on YES
private void ExitMenuItem_Click(object sender, EventArgs e)
{
/*DialogResult result;
result = MessageBox.Show("are you sure?", "exit", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result == DialogResult.Yes)
{
Application.Exit();
}
else
{ return; }*/
}
private void F0101_FormClosing(object sender, FormClosingEventArgs e)
{
DialogResult result;
result = MessageBox.Show(""are you sure?", "exit", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result == DialogResult.Yes)
{
Application.Exit();
}
else
{ e.Cancel = true; }
}
Application.Exit() sends a message to your application to close again, use:
Environment.Exit(0);
Application.Exit Docs:
Informs all message pumps that they must terminate, and then closes
all application windows after the messages have been processed.
Environment.Exit Docs:
Terminates this process and returns an exit code to the operating
system.
So using Environment, the process is terminated. Even you call it inside a try..finally block, the finally block doesn't execute.
However, Application.Exit can be used when you need a chance to execute state saving code in the closing events (which is in your case calling the same method and thus showing the method twice).
I have a main form called Form_Main, when somebody wants to close it, it will close the entire application (by entire application I mean quitting other forms as well). Therefore I prepared a yes/no MessageBox that ask users if they really want to quit the form. Here's where I'm at:
private void Form_Main_FormClosed(object sender, FormClosedEventArgs e)
{
DialogResult result = MessageBox.Show("Are you sure?", "Confirmation", MessageBoxButtons.OKCancel);
if (result == DialogResult.OK)
{
Environment.Exit(1);
}
else
{
//does nothing
}
}
The "OK" button works. But when users clicks "Cancel", Form_Main is closed, but the application is still running (other forms are untouched). What should I replace //does nothing with?
Use the FormClosing event (instead of FormClosed), and then set e.Cancel = true:
private void Form_Main_FormClosing(object sender, FormClosingEventArgs e)
{
var result = MessageBox.Show("Are you sure?", "Confirmation", MessageBoxButtons.OKCancel);
e.Cancel = (result != DialogResult.OK);
}
The FormClosing event occurs before the form actually closes, so you still have a chance to cancel it. By the time you get to the FormClosed event, it's too late.
I am trying to warn users when they want to close the screen if the work is not saved on the screen. Because I dont want them to lose their work. I warn the user with popup saying that "Are you sure that you want to close the screen without saving your work?". When the user clicks YES, it should close the popup and the screen as well. But my popup comes up instantly and user gets stuck with that popup.
here is the code for FormClosing event
private void BudgetSalesFormulaDefinitionFrm_FormClosing(object sender, FormClosingEventArgs e)
{
if (txtFormula.Text != string.Empty || txtFormulName.Text != string.Empty)
{
if (XtraMessageBox.Show("Are you sure that you want to close the screen without saving your work?", "Warning?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
this.DialogResult = System.Windows.Forms.DialogResult.Yes;
this.Close();
}
else {
}
}
}
what am I doing wrong here?
You don't have to close your form when user clicks Yes, we just need to set e.Cancel = true when user clicks NO.
if (txtFormula.Text != string.Empty || txtFormulName.Text != string.Empty){
if (XtraMessageBox.Show("Are you sure that you want to close the screen without saving your work?",
"Warning?",
MessageBoxButtons.YesNo,
MessageBoxIcon.Question) != DialogResult.Yes) {
e.Cancel = true;
}
}
NOTE: You should add condition for CloseReason (got from e.CloseReason) to be sure it's being closed by user.
Hello Guys I have msg box when i press on yes its close that form which calls msg box
how can i do when msg box dialogresult = ok close only himself
Set the DialogResault property to None for the button which its event handler open the MessageBox.
Good luck!
DialogResult result = MessageBox.Show("Click yes to close, otherwise click no.", "Message Box Test", MessageBoxButtons.YesNo);
if (result == DialogResult.Yes)
{
Application.Exit();
}
Maybe you're assigning the result to the DialogResult property of the parent form, (see http://msdn.microsoft.com/en-us/library/system.windows.forms.form.dialogresult.aspx) and in particular from the remark section :
" If the form is displayed as a dialog
box, setting this property with a
value from the DialogResult
enumeration sets the value of the
dialog box result for the form, hides
the modal dialog box, and returns
control to the calling form."
Use:
if (MessageBox.Show(...) == DialogResult.Yes)
{
}
else
{
}