1

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
pallares
  • 160
  • 4
  • 13
Markus G.
  • 331
  • 1
  • 14
  • @downvoter: you should comment why you downvoted my question. Maybe I should add some missing information or other ... Right now, I think it's valid question – Markus G. Apr 04 '17 at 12:38
  • What have you tried in debugging mode? – Danny James Apr 04 '17 at 12:54
  • Could there be an errant keystroke in the keyboard's buffer? How is the dialog triggered? Is it through keyboard or mouse input? What is the Form's `CancelButton` or `AcceptButton` properties set to? – Chris Dunaway Apr 04 '17 at 13:20
  • @ChrisDunaway it is triggered by the download event of the geckofx-framework (e.g. "Public Sub LauncherDialog_Download(ByVal sender As Object, ByVal e As Gecko.LauncherDialogEvent)"); I didn't set the property to other specific settings than mentioned above in the example code-snippet. I guess it's standard then? – Markus G. Apr 04 '17 at 13:39
  • @DannyJames it happens in debugging mode AND when the application runs normal; I tried to debug, but the only effect I could see is, that the application directly returns an Cancel if ShowDialog() is called. No error or anything else is shown – Markus G. Apr 04 '17 at 13:40
  • DannyJames & @ChrisDunaway: In my other question you can see a bit more of the method that is calling this SaveFileDialog-part. I found a workaround for this: I call the saveFileDialog twice then ... but this is groggy coding ... see also https://stackoverflow.com/questions/43209087/savefiledialog-sometimes-throws-an-system-accessviolationexception – Markus G. Apr 04 '17 at 13:49
  • Are you initiating the download on a Mouse Down event perchance? Is it possible that your mouse pointer happens to be where the Cancel button is during Mouse UP? – Chris Dunaway Apr 04 '17 at 14:41
  • 1. why are you setting it to a variable and what data type or object are you using? 2. maybe just put the result straight into an if as follows:- `If dialog.ShowDialog(Me) = DialogResult.OK Then` – Danny James Apr 04 '17 at 15:13
  • @DannyJames Variable is declared as `Dim dialogResultValue As DialogResult` I need to do it this way, so that I can react on the automatic closing and show the dialog again (see referenced question above in my comment); `dialog.ShowDialog(Me)` does not work in vb.net?! – Markus G. Apr 05 '17 at 08:50
  • @chris I'm not initiating on a mouse down event but rather to the download event of geckofx ; `Public Sub LauncherDialog_Download(ByVal sender As Object, ByVal e As Gecko.LauncherDialogEvent)` with `AddHandler Gecko.LauncherDialog.Download, AddressOf LauncherDialog_Download` – Markus G. Apr 05 '17 at 08:55
  • Are you sure the method isn't being called again for whatever reason? You could put a `SyncLock` on just as a test(not for production) which _I think_ blocks the method from being called until it's finished. – Bugs Apr 05 '17 at 13:12

1 Answers1

0

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

Markus G.
  • 331
  • 1
  • 14