none
Thumbnail View in OpenFileDialog / SaveFileDialog

    General discussion

  • Having searched long and hard I came up with nothing to solve this problem.

     

    It appears that no body could give me any direction on how to default an Open Dialog box to what ever view type I desired.

     

    It was after several fruitless hours that I changed my way of thinking and started to look if this had been accomplished in any other language other than VB.

     

    that is when I stumbled on some code in C# written by Robert Rohde (German I think)

    the original code can be found at http://www.codeproject.com/KB/dialog/FileDialogExtender.aspx

     

    I have rewritten his code to be VB2005 compliant and It works a treat, so for all those (who like me) couldnt find how to complete this very simple yet so difficult to achieve task I present the code

     

    put this first section into a class called FileDialogExtender

     

    Code Snippet

    '

    ' Rewritten for VB2005 by Dogs_Bollox

    '

    ' Original Post C#

    ' URL : http://www.codeproject.com/KB/dialog/FileDialogExtender.aspx

    ' by Robert Rohde

    '

    Imports System.Runtime.InteropServices

    Namespace FileDialogExtender

    Public Class FileDialogExtender

    #Region "Enum Dialog View Types"

    Public Enum DialogViewTypes

    Icons = 28713

    List = 28715

    Details = 28716

    Thumbnails = 28717

    Tiles = 28718

    End Enum

     

    #End Region

     

    #Region "Private Variables"

    Private Const WM_COMMAND As Int32 = 273

    Private _LastDialogHandle As Int32 = 0

    Private _ViewType As DialogViewTypes

    Private _Enabled As Boolean

    #End Region

     

    #Region "Dll Imports"

    <DLLIMPORT(< FONT>

    <DllImport("user32.dll", EntryPoint:="SendMessageA", CallingConvention:=CallingConvention.StdCall, CharSet:=CharSet.Ansi)> _

    Private Shared Function SendMessage(ByVal Hdc As UInteger, ByVal Msg_Const As UInteger, ByVal wParam As UInteger, ByVal lParam As UInteger) As UInteger

    End Function

     

    <DllImport("user32.dll", EntryPoint:="FindWindowExA", CallingConvention:=CallingConvention.StdCall, CharSet:=CharSet.Ansi)> _

    Private Shared Function FindWindowEx(ByVal hwndParent As UInteger, ByVal hwndChildAfter As UInteger, ByVal lpszClass As String, ByVal lpszWindow As String) As UInteger

    End Function

    #End Region

     

    #Region "Constructors"

    Public Sub New()

    Me.New(DialogViewTypes.List, False)

    End Sub

     

    Public Sub New(ByVal viewType As DialogViewTypes)

    Me.New(viewType, False)

    End Sub

     

    Public Sub New(ByVal viewType As DialogViewTypes, ByVal enabled As Boolean)

    _ViewType = viewType

    _Enabled = enabled

    End Sub

    #End Region

     

    #Region "Properties"

    Public Property DialogViewType() As DialogViewTypes

    Get

    Return _ViewType

    End Get

    Set(ByVal value As DialogViewTypes)

    _ViewType = value

    End Set

    End Property

     

    Public Property Enabled() As Boolean

    Get

    Return _Enabled

    End Get

    Set(ByVal value As Boolean)

    _Enabled = value

    End Set

    End Property

    #End Region

     

    Public Sub WndProc(ByRef m As Message)

     

    If Not _enabled Then

    Return

    End If

     

    If m.Msg = 289 Then

    'Notify of message loop

    Dim dialogHandle As Integer = CInt(m.LParam)

    'handle of the file dialog

    If dialogHandle <> _lastDialogHandle Then

    'only when not already changed

    'get handle of the listview

    Dim listviewHandle As Integer = CInt(FindWindowEx(CUInt(dialogHandle), 0, "SHELLDLL_DefView", ""))

    'send message to listview

    SendMessage(CUInt(listviewHandle), WM_COMMAND, CUInt(_ViewType), 0)

    'remember last handle

    _lastDialogHandle = dialogHandle

    End If

    End If

    End Sub

     

    End Class

    End Namespace

     

     

    Great stuff now to actually use the new functionality place the following into the calling form

    Code Snippet
     

    Private _extender As FileDialogExtender.FileDialogExtender = New FileDialogExtender.FileDialogExtender

     

    Protected Overloads Overrides Sub WndProc(ByRef m As Message)

    MyBase.WndProc(m)

    _Extender.WndProc(m)

    End Sub

     

    Private Sub ShowFileDialog()

    Dim ofd As New OpenFileDialog

     

    _extender.Enabled = True

    _extender.DialogViewType = FileDialogExtender.FileDialogExtender.DialogViewTypes.Thumbnails

     

    ofd.ShowDialog(Me)

    End Sub

     

     

    Obviously you dont have to write the ShowDialog bit of code, as you can use one dump straight on the form

    but you do have to specify _Extender.enabled = True  if this is not done the over-ride of the view will not take place.

     

    **Important*

    one thing that I have found (Wasnt Easy Either) if you are using child and MDI forms the WndProc sub needs to be in the parent for this all to work.

     

    I hope this is of use to some

    Regard

    Dogs_Bollox

    Friday, January 25, 2008 11:41 AM

All replies

  • Hi,

     

    Thanks very much for your excellent work and sharing! Very Helpful! I will change your post from question type to comment in order that others can understand the thread is helpful without any problems.

     

    Thanks again!

    Tuesday, January 29, 2008 6:54 AM
  • Very nice - exactly what I was looking for! Thank you very much!
    Saturday, June 07, 2008 2:36 PM
  • Hi Dogs_Bollox,

    This is a great piece of code. Great work.  I have a small MDI application that I would like to use this in.  The code works fine in an application that is not MDI.  How can I get this code to work in an MDI application.  How did you get it to work.  I'm not sure how to put the WndProc sub in the MDI parent.  I've tried but it still doesn't work.  My child form opens the dialog but it doesn't show the list in thumbnail view.  I'm using VS 2005 vb.net.

    I'd really appreciate some help with this.

    Thanks,

    millerc

    Wednesday, September 17, 2008 11:46 AM
  • Dogs, Thanks for the file open dialog extender code which worked great until I refactored my application to use an inherited base class for common controls and methods. 

    Before moving any of the previously working openfileextender code around, I get a null reference exception at "_extender.WndProc(m)" in the following code snippet in my subclass:

        Private _extender As FileDialogExtender.FileDialogExtender = _
            New FileDialogExtender.FileDialogExtender

        Protected Overloads Overrides Sub WndProc(ByRef m As Message)
            MyBase.WndProc(m)
            _extender.WndProc(m)
        End Sub

    I tried moving the WndProc subroutine from the FileDialogExtended.FileDialogExtender code module to my base class, but this won't compile due to name scoping.  When you suggested moving WndProc to the base class, did you mean that everything in FileDialogExtended.FileDialogExtender should be moved to the base class?

    Thanks, -BGood

    Wednesday, June 10, 2009 9:04 PM
  • No. I see where several people have taken this approach, but always from a C/C#/C++ implementation. What I liked about the approach Dogs_Bollox and has taken is that it is a VB implementation which works with my VB project and is within the realm of my programming experience.  The Dogs_Bollox code previously worked fine in my project, until I recoded to implement a base class inheritance for some of my common controls and procedures. If you have any suggestions, I would certainly be receptive.

    Thanks,

         -BGood
    Thursday, June 11, 2009 3:47 PM
  • FYI: I think I got the Dogs_Bollox code working with VB inheritance by placing ALL of the FileDialogExtended.FileDialogExtender code module in the inherited base class (but not within the class definition) of the inherited base class and commenting out the following code in the subclass:

        Protected Overloads Overrides Sub WndProc(ByRef m As Message)
            MyBase.WndProc(m)
            _extender.WndProc(m)
        End Sub

    Unfortunately, I am just learning to use VB inheritance and I don't know WHY this works.  So if anyone is willing to explain, I am willing to learn.  Thanks, -BGood

    Thursday, June 11, 2009 4:16 PM
  • FYI: I think I got the Dogs_Bollox code working with VB inheritance by placing ALL of the FileDialogExtended.FileDialogExtender code module in the inherited base class (but not within the class definition) of the inherited base class and commenting out the following code in the subclass:

        Protected Overloads Overrides Sub WndProc(ByRef m As Message)
            MyBase.WndProc(m)
            _extender.WndProc(m)
        End Sub

    Unfortunately, I am just learning to use VB inheritance and I don't know WHY this works.  So if anyone is willing to explain, I am willing to learn.  Thanks, -BGood


    Darn!  I made some additional changes in the code-behind and now the openfiledialog no longer opens up in thumbnail view. Again, not sure why.

    If anyone has any suggestions, please let me know.  Thanks, -BGood
    Tuesday, June 16, 2009 3:24 PM