Control File View from OpenFileDialog?

codefinger used Ask the Experts™
When I open a choose file dialog from my program, the files are always listed with the LIST view.  Even if I change the view to my personal preference of DETAILS, the next time my program launches the view is set to LIST again.   Is there a way to control the view provided by OpenFileDialog?  

Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2010

There is no way to set specific dialog to a specific view type. Windows has its own internal way of thinking about how the dialog should be displayed. For example if the special folders like My Pictures has registry settings that when the dialog is opened to that special location it automatically changes the view type to Thumbnails view.

You can however directly send a message to the dialog to make it switch views. If you run SPYX and subclass the dialog you can catch which message it sends when you switch views. Then you send this message directly should make it switch to the appropriate view.
SendMessage using WM_COMMAND for the message and the value (&H702D) for wParam. Sends this directly to dialog class SHELLDLL_DefView



You are getting just a little outside my coding comfort zone.  

Can you provide a code sample please?


Top Expert 2010

Sure give this a whirl..
Example usage:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim dlg As New OpenFileDialog
dlg.Title = "Open Stuff"
CustomDialogView.SetDialogView(CustomDialogView.ViewFlags.Thumbnails, dlg.Title)
'CustomDialogView.SetDialogView(CustomDialogView.ViewFlags.Thumbnails, dlg.Title, 500)
End Sub

Public Class CustomDialogView
    ' egl1044
    Private Const WM_COMMAND As Integer = &H111
    Private Const DefView_ID As Integer = &H461
    Public Enum ViewFlags
        Icons = &H7029
        List = &H702B
        Details = &H702C
        Thumbnails = &H702D
        Tiles = &H702E
    End Enum
    <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
    Private Shared Function SendDlgItemMessage(ByVal hDlg As IntPtr, ByVal nIDDlgItem As Integer, ByVal Msg As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
    End Function
    <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
    Private Shared Function FindWindow(ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
    End Function
    Public Shared Sub SetDialogView(ByVal flags As ViewFlags, _
                                    ByVal title As String, _
                                    Optional ByVal dueTime As Integer = 1000)
        Dim stateObj As New MyState
        stateObj.Flags = flags
        stateObj.Title = title
        Dim TimerCb As New Timer(New TimerCallback(AddressOf SetDialogViewCallback), _
                                 stateObj, dueTime, Timeout.Infinite)
    End Sub
    Private Shared Sub SetDialogViewCallback(ByVal state As Object)
        Dim stateObj As MyState = DirectCast(state, MyState)
        Dim hWindow As IntPtr = IntPtr.Zero
        hWindow = FindWindow(Nothing, stateObj.Title)
        If hWindow.ToInt32 <> 0 Then
            SendDlgItemMessage(hWindow, DefView_ID, WM_COMMAND, _
                               New IntPtr(stateObj.Flags), IntPtr.Zero)
        End If
    End Sub
    Private Class MyState
        Public Title As String
        Public Flags As ViewFlags
    End Class
End Class

Open in new window

Most Valuable Expert 2012
Top Expert 2014



Hard to believe one has to work so hard to accomplish such a simple result.   Thanks!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial