SaveFileDialog closes automatically directly after calling showDialog()

Markus G.

I'm using Windows 7 with Visual Studio 2013.

My application is a webbrowser-Component with GeckoFx. At the download-event I trigger to open the SaveFileDialog as follows. But in some cases the dialog disappears directly after callong ShowDialog() and returns a DialogResult.Cancel which jumps into the else-statement, although nobody pressed cancel. No error is thrown.

Any suggestions why this happens here? I've no clue about this ... :-(

        'Save file dialog
        Dim saveFileDialog1 As New SaveFileDialog()

        saveFileDialog1.Filter = "CSV file (*.csv)|*.csv|All files (*.*)|*.*"
        saveFileDialog1.FilterIndex = 2
        saveFileDialog1.RestoreDirectory = True
        saveFileDialog1.FileName = e.Filename
        saveFileDialog1.AutoUpgradeEnabled = False
        saveFileDialog1.CheckPathExists = False
        saveFileDialog1.InitialDirectory = globalParameters.getDownloadDirectory() 'globalParameters._downloadDirectory

        dialogResultValue = saveFileDialog1.ShowDialog()

        If dialogResultValue = DialogResult.OK Then
            'should go on here first, if user presses okay
        Else
            ' I am coming to this point, althoug nobody pressed any cancel button or any other input had happened yet
        End If
Markus G.

Thx for your advises @DannyJames and @ChrisDunaway.

Somehow I could figure out (with both of my questions and your answers) that the SaveFileDialog.ShowDialog(Me) needs a reference to the Form Me.

Only then the SaveFileDialog will load properly without errors or even cancels its call without any other user-action.

Unfortunately I put the download-part into a vb-class that hasn't been inherited by Inherits System.Windows.Forms.Form, so that it hasn't a reference to a Form (which obviously needs to be required).

I changed my code, so that I have references to forms (so, that I could use the reference to a form, such as Me in form-class). And it works like a charm.

For completion here's such an example:

Imports System.IO
Imports Gecko
Imports System
Imports System.Windows.Forms
Imports System.Drawing.Printing
Imports System.Management
Imports System.Threading
Imports System.Runtime.InteropServices
Imports System.Timers

Public Class frmMain

' [...] 
' ATTENTION, MORE CODE IS NEEDED TO RUN GECKOFX WITH AN URL BUT NOT DISPLAYED HERE AT THIS POINT, 
' SINCE IT ISN'T NEEDED HERE TO SHOW THE ACTUAL PROBLEM

''' <summary>
''' Startup-Functionalities, such as Gecko Xpcom-Start etc.
''' </summary>
''' <remarks></remarks>
Public Sub New()
    ' call initiliazer
    InitializeComponent()
    AddHandler Gecko.LauncherDialog.Download, AddressOf Me.LauncherDialog_Download
End Sub

''' <summary>
''' see also
''' http://quabr.com/19906621/how-to-handle-downloads-on-gecko15-with-mozilla-xul15-in-visual-basic
''' or
''' http://stackoverflow.com/questions/19906621/how-to-handle-downloads-on-gecko15-with-mozilla-xul15-in-visual-basic
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
''' <remarks></remarks>
Public Sub LauncherDialog_Download(ByVal sender As Object, ByVal e As Gecko.LauncherDialogEvent)

    Try
        Dim P As String = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) & Path.DirectorySeparatorChar & "tmp" 'globalParameters._downloadDirectory '
        If Not System.IO.Directory.Exists(P) Then System.IO.Directory.CreateDirectory(P)

        Dim objTarget As nsILocalFile = Xpcom.CreateInstance(Of nsILocalFile)("@mozilla.org/file/local;1")

        Using tmp As New nsAString(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + vbTab & "temp.tmp")
            objTarget.InitWithPath(tmp)
        End Using

        If globalParameters._doNotShowDownloadPrompt Then
            'only if user does not want to load saveFileDialog; not interesting at this point
        Else
            'Save file dialog
            Dim saveFileDialog1 As New SaveFileDialog()
            saveFileDialog1.Filter = "CSV file (*.csv)|*.csv|All files (*.*)|*.*"
            saveFileDialog1.FilterIndex = 2
            saveFileDialog1.RestoreDirectory = False
            saveFileDialog1.FileName = e.Filename
            saveFileDialog1.AutoUpgradeEnabled = False
            saveFileDialog1.CheckPathExists = False
            saveFileDialog1.InitialDirectory = globalParameters.getDownloadDirectory() 'globalParameters._downloadDirectory

            Dim dialogResultValue As DialogResult
            Try
                dialogResultValue = saveFileDialog1.ShowDialog(Me)
            Catch ex As Exception
                logging.logInformation("Probleme beim laden des Dialogs: " & ex.ToString())
            End Try

            If dialogResultValue = DialogResult.OK Then
                Try
                    Dim par As New Parameters
                    par.sender = sender
                    par.e = e
                    par.mime = e.Mime
                    par.url = e.Url
                    par.fileName = saveFileDialog1.FileName
                    par.dialogResultValue = dialogResultValue
                    par.myStream = saveFileDialog1.OpenFile()
                    modMain.ThreadJob(par)
                Catch ex As Exception
                    logging.logInformation("Error during loading File" & e.ToString)
                End Try
            End If
        End If

    Catch ex As Exception
        logging.logInformation("Error during loading File" & ex.ToString)
    Finally
        ' nothing to to here
    End Try
End Sub


Private Sub frmMain_Disposed(sender As Object, e As EventArgs) Handles Me.Disposed
    RemoveHandler Gecko.LauncherDialog.Download, AddressOf Me.LauncherDialog_Download
End Sub
End Class

I hope I could describe the problem properly for other people searching this problem

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Dialog closes directly after open

From Dev

Message box closes automatically after a brief delay

From Dev

Firebase popup window closes after calling login

From Dev

CMD closes after calling cordova command

From Dev

Firebase popup window closes after calling login

From Dev

Dropdown field automatically closes after opening in IE11

From Dev

java joptionpane which automatically closes after few seconds

From Dev

Tkinter window closes automatically after Python program has run in PyCharm

From Dev

go to previous window automatically after printing dialog closes

From Dev

java joptionpane which automatically closes after few seconds

From Dev

Tkinter window closes automatically after Python program has run in PyCharm

From Dev

Dropdown field automatically closes after opening in IE11

From Dev

Application closes automatically

From Dev

Putty - Automatically closes on authentication

From Dev

NSPopover closes automatically

From Dev

Putty - Automatically closes on authentication

From Dev

Spring Tool Suite Closes Automatically after adding dependency to pom.xml file

From Dev

Widget opens for a second then automatically closes?

From Dev

FCM push notification closes automatically

From Dev

custom jquery alert closes automatically

From Dev

App closes after posting 'č','š' and 'ž'

From Dev

Calling event handler directly

From Dev

Calling event handler directly

From Dev

Calling function directly in for loop

From Dev

VB - Form 2 closes automatically whenever Form 1 closes?

From Dev

Form hides behind the other form after ShowDialog()

From Dev

Do an action from ViewModel right after ShowDialog

From Dev

Form hides behind the other form after ShowDialog()

From Dev

How to make what the menu will automatically closes?

Related Related

  1. 1

    Dialog closes directly after open

  2. 2

    Message box closes automatically after a brief delay

  3. 3

    Firebase popup window closes after calling login

  4. 4

    CMD closes after calling cordova command

  5. 5

    Firebase popup window closes after calling login

  6. 6

    Dropdown field automatically closes after opening in IE11

  7. 7

    java joptionpane which automatically closes after few seconds

  8. 8

    Tkinter window closes automatically after Python program has run in PyCharm

  9. 9

    go to previous window automatically after printing dialog closes

  10. 10

    java joptionpane which automatically closes after few seconds

  11. 11

    Tkinter window closes automatically after Python program has run in PyCharm

  12. 12

    Dropdown field automatically closes after opening in IE11

  13. 13

    Application closes automatically

  14. 14

    Putty - Automatically closes on authentication

  15. 15

    NSPopover closes automatically

  16. 16

    Putty - Automatically closes on authentication

  17. 17

    Spring Tool Suite Closes Automatically after adding dependency to pom.xml file

  18. 18

    Widget opens for a second then automatically closes?

  19. 19

    FCM push notification closes automatically

  20. 20

    custom jquery alert closes automatically

  21. 21

    App closes after posting 'č','š' and 'ž'

  22. 22

    Calling event handler directly

  23. 23

    Calling event handler directly

  24. 24

    Calling function directly in for loop

  25. 25

    VB - Form 2 closes automatically whenever Form 1 closes?

  26. 26

    Form hides behind the other form after ShowDialog()

  27. 27

    Do an action from ViewModel right after ShowDialog

  28. 28

    Form hides behind the other form after ShowDialog()

  29. 29

    How to make what the menu will automatically closes?

HotTag

Archive