4
votes

I have a dialog box popup in a VSTO addin for outlook 2013. I test for DialogResult.Yes and No, to which I have set the two buttons' results. They work fine, but I want yet another behavior when the user cancels out of the box. When they press cancel the code just continues. Is there something I can call to stop the addin from executing if they cancel the dialog box? How can I test for the cancel button? I tried res == DialogResult.Cancel but it can't cast res to bool and it's type DialogResult because I also test for Yes and No.

How can I tell if they press the cancel button, and how can I exit the addin. In python the command would be sys.exit() What is the C# equivalent?

1

1 Answers

7
votes

If you use the System.Windows.Forms.MessageBox class for displaying dialog boxes in the add-in you may use the following code for checking the chosen option:

// Display message box
DialogResult result = MessageBox.Show(messageBoxText, caption, button, icon);

// Process message box results 
switch (result)
{
    case MessageBoxResult.Yes:
        // User pressed Yes button 
        // ... 
        break;
    case MessageBoxResult.No:
        // User pressed No button 
        // ... 
        break;
    case MessageBoxResult.Cancel:
        // User pressed Cancel button 
        // ... 
        break;
 } 

See Dialog Boxes Overview in MSDN for more information.

If you developed your own window you may add an event handler for the button's Click event.