0

I have a common dialog opened like this:

private void SaveLogButton_Click(object sender, EventArgs e)
{ 
    try
    {
        SaveFileDialog dialog = new SaveFileDialog
        {
            Filter = @"Text file|*.txt",
            Title = @"Save to...",
        };

        if (dialog.ShowDialog()== DialogResult.OK)
        {
            // Do some job
        }
    }
    catch (Exception ex)
    {
        // Handle some errors
    }
}

Sometimes, however, (with like 10% probability) ShowDialog() method does not show the dialog itself, though I can see it's parent form, which i can't click too (error sound appears). The only thing that helps here is CtrlAltDel.

No exceptions trigger, and debug wont go after ShowDialog line. Any suggestions?

Thank you.

Short addition: I'm working on an Excel Add-In using WinForms.

Raktim Biswas
  • 4,011
  • 5
  • 27
  • 32
Ilya Bezus
  • 140
  • 10
  • 1
    Does it exist behind the main form? Is this a multi-monitor setup? If you hover your mouse over the icon in the task bar, does it show both windows? – Chris Dunaway Sep 13 '16 at 21:37
  • A dialog always requires an *owner* window. A window it can be on top of and return the focus to when it closes. ShowDialog() has a convenience overload that doesn't require you to state the owner explicitly. Works just about always, it can figure out who the owner should be from calling GetActiveWindow(). Except when it returns null, then it is the desktop window becomes the owner. Uh-oh. Ctrl+Alt+Del time might well be inspired after it disabled *every* window that's owned by the desktop window :) Never skip selecting the owner in any interop scenario. – Hans Passant Sep 13 '16 at 22:21
  • https://blogs.msdn.microsoft.com/oldnewthing/20040224-00/?p=40493 – Hans Passant Sep 13 '16 at 22:23
  • found my solution here: http://stackoverflow.com/questions/39257321/how-to-debug-hanging-savefiledialog – Ilya Bezus Sep 14 '16 at 07:49

1 Answers1

0

Use this 'ShowDialog' overload https://msdn.microsoft.com/en-us/library/9a55b9ds(v=vs.110).aspx to specify Owner.

if (dialog.ShowDialog(this)== DialogResult.OK)
shfire
  • 837
  • 7
  • 7