2017-04-04 30 views
1

我使用Windows 7和Visual Studio 2013年SaveFileDialog调用的ShowDialog后直接自动关闭()

我的应用程序是一个网页浏览器,组件与GeckoFx。在下载事件中,我触发打开SaveFileDialog,如下所示。但在某些情况下,对话框会在callong ShowDialog()之后直接消失,并返回跳转到else语句的DialogResult.Cancel,但没有人按下取消。 没有错误发生。

任何建议为什么这里发生?我有没有这方面的线索...... :-(

 '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 
+0

@downvoter:你应该评论你为什么downvoted我的问题。也许我应该添加一些缺失的信息或其他...现在,我认为这是有效的问题 –

+0

你在调试模式下尝试过什么? –

+0

键盘缓冲区中是否有错误的按键?对话如何触发?它是通过键盘还是鼠标输入?什么是窗体的“CancelButton”或“AcceptButton”属性设置为? –

回答

0

THX为你的建议@DannyJames和@ChrisDunaway。

不知怎的,我能想出(既我的问题和你的应答)在SaveFileDialog.ShowDialog(Me)需要将表格Me参考。

才把SaveFileDialog会正常加载没有错误或没有任何其它用户动作,甚至取消其呼叫。

不幸的是,我把下载的部分成一个没有被Inherits System.Windows.Forms.Form继承的vb类,所以它没有对表单的引用(这显然需要被需要)。

我改变了我的代码,以便引用表单(所以,我可以使用表单的引用,例如表单类中的Me)。它就像一个魅力。

完成这里是这样一个例子:

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 

我希望我能正确地描述问题的其他人搜索这个问题