Setting the start position for OpenFileDialog/SaveFileDialog

前端 未结 8 1713
野趣味
野趣味 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

    Using Rob Sherrit's response on Jan 22 '14 as inspiration, I created a new module and called it CKRFileDialog (call it what you want) which contains the following code:

    Public Function Show(fd As Object, CoveredForm As Form, Optional bShowHelp As Boolean = False) As DialogResult
    
        Dim oDR As DialogResult
    
        'The .Net FileDialogs open in the last Form that was created. 
        'To ensure they appear in the area of the current Form, we create a new HIDDEN PositionForm and then 
        'delete it afterwards.
    
        Dim PositionForm As New Form With {
          .StartPosition = FormStartPosition.Manual,
          .Left = CoveredForm.Left + CInt(CoveredForm.Width / 8),  'adjust as required
          .Top = CoveredForm.Top + CInt(CoveredForm.Height / 8),   'adjust as required
          .Width = 0,
          .Height = 0,
          .FormBorderStyle = Windows.Forms.FormBorderStyle.None,
          .Visible = False
        }
        PositionForm.Show()
    
        'If you use SkyDrive you need to ensure that "bShowHelp" is set to True in the passed parameters.
        'This is a workaround for a problem on W8.1 machines with SkyDrive installed.
        'Setting it to "true" causes the "old" W7 control to be used which avoids a pointing to SkyDrive error.
        'If you do not use SkyDrive then simply do not pass the last parameter (defaults to "False")
        fd.ShowHelp = bShowHelp
    
        'store whether the form calling this routine is set as "topmost"
        Dim oldTopMost As Integer = CoveredForm.TopMost
        'set the calling form's topmost setting to "False" (else the dialogue will be "buried"
        CoveredForm.TopMost = False
    
        oDR = fd.ShowDialog(PositionForm)
    
        'set the "topmost" setting of the calling form back to what it was.
        CoveredForm.TopMost = oldTopMost
        PositionForm.Close()
        PositionForm.Dispose()
        Return oDR
    
    End Function
    

    I then call this code in my various modules as follows:

    If performing a "FileOpen" ensure that there is a FileOpenDialog component added to your form or code and adjust the properties of the component if you wish (e.g. InitDirectory,Multiselect,etc.)

    Do the same when using FileSaveDialog components (Different properties to the FileOpenDialog component may apply).

    To "show" the dialog component use a line of code as follows, passing two parameters, the first the FileDialog you are using ("Open" or "Save") and the second parameter the Form upon which you wish to overlay the dialogue.

    CKRFileDialog.Show(saveFileDialog1, CoveredForm) or CKRFileDialog.Show(openFileDialog1, CoveredForm)

    Remember, if you are using SkyDrive you must pass "True" as a third parameter:

    CKRFileDialog.Show(saveFileDialog1, CoveredForm, True) or CKRFileDialog.Show(openFileDialog1, CoveredForm, True)

    I set the "offset" of the dialogue to be 1/8 the way across and down on the form "CoveredForm", but you can set that back to 1/2 (as in Rob Sherret's code) or whatever value you wish.

    This seemed the easiest approach

    Thanks Rob! :-)

提交回复
热议问题