Setting the start position for OpenFileDialog/SaveFileDialog

前端 未结 8 1638
野趣味
野趣味 2021-01-01 11:48

For any custom dialog (form) in a WinForm application I can set its size and position before I display it with:

form.StartPosition = FormStartPosition.Manual         


        
8条回答
  •  独厮守ぢ
    2021-01-01 12:05

    Very grateful for BobB's reply on this one. There are a few more "gotchas". You have to pass the handle of PositionForm when calling OpenFileDialog1.ShowDialog(PositionForm) otherwise BobB's technique is not reliable in all cases. Also, now that W8.1 launches a new fileopen control with SkyDrive in it, the Documents folder location in the W8.1 fileopen control is now screwed. So I frig fileopen to use the old W7 control by setting ShowHelp = True.

    Here is the VB.NET code I ended up using, my contribution to the community in case it helps.

    Private Function Get_FileName() As String
    
        ' Gets an Input File Name from the user, works with multi-monitors
    
        Dim OpenFileDialog1 As New OpenFileDialog
        Dim PositionForm As New Form
        Dim MyInputFile As String
    
        ' The FileDialog() opens in the last Form that was created.  It's buggy!  To ensure it appears in the
        ' area of the current Form, we create a new hidden PositionForm and then delete it afterwards.
    
        PositionForm.StartPosition = FormStartPosition.Manual
        PositionForm.Left = Me.Left + CInt(Me.Width / 2)
        PositionForm.Top = Me.Top + CInt(Me.Height / 2)
        PositionForm.Width = 0
        PositionForm.Height = 0
        PositionForm.FormBorderStyle = Forms.FormBorderStyle.None
        PositionForm.Visible = False
        PositionForm.Show()
    
        ' Added the statement "ShowHelp = True" to workaround a problem on W8.1 machines with SkyDrive installed.
        ' It causes the "old" W7 control to be used that does not point to SkyDrive in error.
    
        OpenFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
        OpenFileDialog1.Filter = "Excel files (*.xls*)|*.xls*|CSV Files (*.csv)|*.csv"
        OpenFileDialog1.FilterIndex = 1
        OpenFileDialog1.RestoreDirectory = True
        OpenFileDialog1.AutoUpgradeEnabled = False
        OpenFileDialog1.ShowHelp = True
        OpenFileDialog1.FileName = ""
        OpenFileDialog1.SupportMultiDottedExtensions = False
        OpenFileDialog1.Title = "Select an Excel or .csv file containing patent data or list of Publication Numbers for your project."
    
        If OpenFileDialog1.ShowDialog(PositionForm) <> System.Windows.Forms.DialogResult.OK Then
            Console.WriteLine("No file was selected. Please try again!")
            PositionForm.Close()
            PositionForm.Dispose()
            OpenFileDialog1.Dispose()
            Return ""
        End If
        PositionForm.Close()
        PositionForm.Dispose()
    
        MyInputFile = OpenFileDialog1.FileName
        OpenFileDialog1.Dispose()
        Return MyInputFile
    
    End Function
    

提交回复
热议问题