Search code examples
c#winformsdialogresult

Start a process with DialogResult c#


I am actually writing a program that creates specific excel files I need, with Microsoft.Office.Interop.Excel. This works fine.

My program creates, then safes and close the new excel file (works fine).

 sheet.SaveCopyAs(path);
 sheet.Saved = true;
 sheet.Close(true, misValue, misValue);
 excel.Quit();

When the new Excel File was created successfully a DialogResult Box opens and asks if I want to open the new Excel file or not

DialogResult dr = MessageBox.Show("Open new file?", "text", MessageBoxButtons.YesNo);
          {
             if (DialogResult == DialogResult.Yes)
             {
                  Process.Start(path);
             }
             else if (DialogResult == DialogResult.No)
             {                          
                   this.Close();
             }

But when I press YES, nothing happens, the new file does not open.

I tried it with an extra button on my form

 private void button4_Click(object sender, EventArgs e)
        {
            Process.Start(path);
        }

This way works, but why is the DialogResult Box not opening my new Excel File?


Solution

  • Your dialog Result value is stored in dr, so you should compare the dr:

    DialogResult dr = MessageBox.Show("Open new file?", "text",  
                                      MessageBoxButtons.YesNo);
    if (dr == DialogResult.Yes)
    {
        Process.Start(path);
    }
    else if (dr == DialogResult.No)
    {                          
         this.Close();
    }