3

I have an issue with dialog forms. Below is a section of C# code which calls the FolderBrowserDialog window. Now when I click "ok" on a folder it will close the dialog so not concerned about that so much. However does anyone know how to detect the cancel event? I have tried looking it up but all I seem to be able to find is "dismiss." Not sure that can help me.

    private void link1add_Click(object sender, EventArgs e)
    {
        FolderBrowserDialog dialog = new FolderBrowserDialog();
        dialog.ShowDialog(); // Opens Folderdialog
    }

For example something along these lines:

    private void link1add_Click(object sender, EventArgs e)
    {
        FolderBrowserDialog dialog = new FolderBrowserDialog();
        dialog.ShowDialog(); // Opens Folderdialog
        if (dialog == dialog.Cancel)            
        {
        }
    }

If anyone could shed some light on this I would be very greatful. Thank you for looking.

Marshal
  • 1,177
  • 4
  • 18
  • 30

4 Answers4

8

Try This:

private void link1add_Click(object sender, EventArgs e) {
        FolderBrowserDialog f = new FolderBrowserDialog();

        if (f.ShowDialog() == System.Windows.Forms.DialogResult.Cancel) {
          //handle Cancel
        }
    }
ionden
  • 12,536
  • 1
  • 45
  • 37
  • This worked! Quite frustrating though since I thought I tried this. Thank you very much for your help I can stop pulling my hair out now. – Marshal Mar 05 '12 at 13:33
2
private void link1add_Click(object sender, EventArgs e)
{
  DialogResult dr = dialog.ShowDialog();
  If( dr == DialogResult.Ok)
  {

  } 
  else
  {
      //All other situations
  }
 }
CodingBarfield
  • 3,392
  • 2
  • 27
  • 54
2
 FolderBrowserDialog dialog = new FolderBrowserDialog();
 var res = dialog.ShowDialog();
 if(res == System.Windows.Forms.DialogResult.OK)
 {

 }
 else
 {
     //dialog.Cancel
 }
Akrem
  • 5,033
  • 8
  • 37
  • 64
1

There is no event you need to handle. Just look at the return value of ShowDialog():

var result = dialog.ShowDialog();
if ( result == DialogResult.Cancel )
{
...
}
Chris Barlow
  • 3,274
  • 4
  • 31
  • 52
jlew
  • 10,491
  • 1
  • 35
  • 58