How to close only the YesNoCancel Dialog Box on clicking Cancel Button,keeping the background project visible

1.3k Views Asked by At

I have a dialog box with Yes, No and Cancel clicking Yes save the currently created project, On clicking the Cancel button should just close the Dialog box doing no action.

private void closeproject_click(DialogResult DResult)
{
    if(MessageBox.Show("Do you want to save the project and close tool? 
             MessageBoxButtons.YesNoCancel) ==  DialogResult.Yes))
    {
        //Save the current Project if not saved and close 
    }
    else if("Do you want to save the project and close tool? 
             MessageBoxButtons.YesNoCancel) ==  DialogResult.No))
    {
        //doesn't save the project and get closes
    }
    else if("Do you want to save the project and close tool? 
             MessageBoxButtons.YesNoCancel) ==  DialogResult.Cancel)
    {
        // should close the dialog box form only 
        // what do i write
    }

Complete Tool should not get closed, only the dialog should close on clicking the Cancel Button and kill the background process.

2

There are 2 best solutions below

4
Hammas On

You should use simple approach to perform what you are trying to do.

private void TestButton_OnClick(object sender, RoutedEventArgs e)
{
    MessageBoxResult result = MessageBox.Show("My Message Question", "My Title", MessageBoxButton.YesNoCancel, MessageBoxImage.Question);
    if (result == MessageBoxResult.Yes)
    {
        //Saveproject(); // write your save code here or call saving method and close the whole project
        Close();
    }
    else if (result == MessageBoxResult.No)
    {
        Close(); // dont do anything just close project
    }
    else if (result == MessageBoxResult.Cancel)
    {
        // you dont even need this third condition on cancel btn click only message box will close 
        //and nothing else will happen  
       // additionally you can use here
          e.Cancel = true;  
          e.Handled = true;
    }
}
0
Sam Xia On

If you want to do nothing, just write nothing in that condition. Let the event simply return, then it does nothing.