Results 1 to 11 of 11

Thread: OpenFileDialog Component with preselect Thumbnails view, Details view etc.

Threaded View

  1. #1

    Thread Starter
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    OpenFileDialog Component with preselect Thumbnails view, Details view etc.

    An enhanced OpenFileDialog you can put into your Toolbox
    If you're a graphics nut like me, the standard OpenFileDialog can be pretty irritating because it always opens in List view, when really you want Thumbnails view. So I wanted to make an OpenFileDialog where you can set the opening view to Thumbnails, Icons, Details or whatever, instead of having to change it every time you open it.

    How it was made
    It isn't possible to subclass the standard OpenFileDialog because it's NonInheritable. But I found an article on the Code Project where the author, Robert Rohde, had a clever idea. He found a way to intercept a message that the dialog passes to its owner form, and used information form that message to set the view mode. The snag is that you had to code everything on the Form itself, because the message concerned, WM_ENTERIDLE, is only detected by the WndProc sub on the form itself (and not by an IMessageFilter, for instance). This seemed rule out putting the method into a simple component you could just drag onto your form. But I found a way to do it.

    My solution consists of two main parts. Firstly, there is a Class which inherits from IComponent. The component exposes its own version of the OpenFileDialog properties which you can set in the Forms Designer. It has an additional DefaultView property, which you use to set the opening view of the dialog. Like the original OpenFileDialog component, it also has ShowDialog method which returns a DialogResult.

    The OFDForm is the second part. It's a Form which is called from the component's ShowDialog and it does most of the work. It has its own copy of the standard OpenFileDialog, which it displays, collects the results and then closes. At the same time, the form has a WndProc sub which picks up the WM_ENTERIDLE message and uses it to set the view to Thumbnails, Icons or whatever you chose using SendMessage. The OFD form is itself invisible: all you see is the Dialog. I made the form's Opacity = 0% and its size to 1*1 pixels because just hiding it wouldn't work (WndProc wouldn't fire).

    The attachments contain the above two parts plus a toolbox icon.

    How to build the project in Visual Studio
    Use this if you want to study the code or modify it. I'll see if I can post the DLL to the Utilities forum so you can skip these steps.

    1. Download the attachments below to a convenient folder.
    2. Start a new Control Library project in Visual Studio.
    3. Right-click on the project name in Solution Explorer, and select Add Existing Item ... Use this to add all the downloaded items.
    4. Delete the default empty class.
    5. Right-click on the project name in Solution Explorer and select Properties.
    5a. On the Compile tab, browse for a folder where you want to store the DLL. That's where it will live if you put it in the Toolbox.
    5b. On the References tab, make sure you have references to System.Drawing and System.Windows.Forms.
    6. Right click on the bitmap (OpenFileDialogEx.bmpP in Solution Explorer. Check that its Build Action is set to Embedded Resource.
    7. Build the Solution.

    How to add the component to your toolbox
    1. Open any Windows Forms project and view the Form.
    2. Open the Toolbox and right click wherever you want to put the new comonent. Select Choose Items ...
    3. Click the Browse button and browse to the folder where you have stored the DLL (step 5b above). Select it. The OpenFileDialogEx component should now appear in your Toolbox with the following icon: Name:  OpenFileDialogEx.bmp
Views: 4925
Size:  822 Bytes.

    Now you can drag the component onto a form and set its properties in the same way as a normal OpenFileDialog. Except it now has a new DefaultView property. Alternatively you could use it in code like this, for example:
    Code:
       Using ofdx As New BBComponents.OpenFileDialogEx
                If ofdx.ShowDialog = DialogResult.OK Then
                    zoomablepicturebox1.Image = Image.FromFile(ofdx.FileName)
                End If
            End Using
    Comments and suggestions gladly received. BB
    Attached Files Attached Files

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width