Dailycode.info

Short solution for short problems

FileDialog default settings.

 

I was using several ‘Open’ and ‘Save’ file dialogs on my screen, all for importing or exporting xml files. Instead if setting the default parameters for each dialog over again, I made this easy function that helps to set the default parameters:

    Private Shared Sub DialogInit(ByRef dialog As FileDialog)

        dialog.InitialDirectory = Environment.SpecialFolder.Desktop

        dialog.DefaultExt = "xml"

        dialog.Filter = "XML|*.xml"

    End Sub

 

Then all file dialogs call this method to set the default parameters and all dialogs act the same:

Using dialog As New OpenFileDialog

'dialog.InitialDirectory = Environment.SpecialFolder.Desktop 

           'dialog.DefaultExt = "xml"

           'dialog.Filter = "XML|*.xml"

           DialogInit(dialog)

           If dialog.ShowDialog() <> DialogResult.OK Then Return

           xmlFile = dialog.FileName

End Using

 

Another example:

Using dialog As New SaveFileDialog()

DialogInit(dialog)

           If dialog.ShowDialog() <> DialogResult.OK Then Return

           xmlFile = dialog.FileName

End Using