Click here to Skip to main content
12,547,501 members (46,851 online)
Click here to Skip to main content
Add your own
alternative version


74 bookmarked


, 4 Dec 2004 CPOL
Rate this:
Please Sign up or sign in to vote.
Describes an easy way to change the initial view state of the listview in the Open/SaveFileDialog.

Sample Image - article.jpg


The .NET framework is in my opinion an impressive piece of work with many useful classes. But I think every developer has at least once faced the problem that a class or function doesn't exactly fit his needs. Normally, one would derive from it and extend the functionality. The OpenFileDialog and SaveFileDialog are sealed and thus there is no direct way of extending them. This article explains how you can extend them anyway without the need to make everything yourself.


Once upon a time, in a country far far away...

... a customer had the request that in an OpenFileDialog - where he could open an image - the default view state of the contained listview should be the thumbnail view. No one in my company really thought about it, and so we just said: "No problem!".

After looking at the given dialog classes, I realized that this wasn't as easy as just setting the appropriate property. I searched the forums and several search engines, but all I could find was listings which showed how to create the dialogs directly via the Windows APIs which needed dozen of structs with dozen of fields and cryptic constants. This was just too much overhead for me just to change the initial view state. So I began to think about other solutions.

The idea which came to my mind was that the parent form somehow must get to know when and which modal dialog is displayed. So I wrote a form overriding the WndProc and traced the messages coming while opening a file dialog. After some minutes, I had the message I needed. It has the number 289 and fires in the message loop of the modal dialog. This message also contains the handle to the dialog. It is important that the dialog is fully created at this time, because otherwise the following steps wouldn't work.

Phew! No, I had the handle to the dialog and knew it was shown (I could just have looked at the screen to realize this). Now, I needed a way to change the style of the listview. So I started Spy++ and searched for the dialog (with the handle at hand, this was easy) and quickly found the child window which was containing the listview.

SampleImage - spy1.jpg

I opened the messages dialog of Spy++ and began changing the listview's view state, and after some filtering, I got the message which was changing the style. The appropriate parameters for the view types were also shown by Spy++.

SampleImage - spy2.jpg

Now, I just had to add two small API calls to change my dialog. I used FindWindowEx to search for the handle of the window holding the listview. After retrieving it, I called SendMessage to send the message delivered by Spy++ to the window.

Using the code

I tried to stuff everything into a component. The problem is that the only one who has access to WndProc of a System.Windows.Form is the form itself. Thus, the form that will show the file dialog needs to override WndProc and make a call to this component. The minimal code to show an extended dialog would be:

public class TestForm : System.Windows.Forms.Form
    private System.Windows.Forms.Button _btnOpen;
    private FileDialogExtender _extender = 
        new FileDialogExtender();

    public TestForm()
        this._btnOpen = 
            new System.Windows.Forms.Button();
        this._btnOpen.Location = 
            new System.Drawing.Point(8, 8);
        this._btnOpen.Name = "_btnOpen";
        this._btnOpen.Size = 
            new System.Drawing.Size(96, 23);
        this._btnOpen.Text = "Open";
        this._btnOpen.Click += 
            new System.EventHandler(this.ShowOpenDialog);
        this.FormBorderStyle = 
        this.MaximizeBox = false;
        this.MinimizeBox = false;
        this.Name = "TestForm";
        this.Text = "Test";

    static void Main() 
        Application.Run(new TestForm());

    private void ShowOpenDialog(object sender, 
        System.EventArgs e)
        new OpenFileDialog().ShowDialog();

    protected override void WndProc(ref Message m)
        base.WndProc(ref m);
        _extender.WndProc(ref m);

A better sample project is included in one of the download links.

Points of Interest

I hope I could show you how you can extend system dialogs by finding the needed handles to remote control their behavior.

There is much more one could do this way without throwing away the benefits of using the managed dialog classes. As I have no further need for extensions, I will now leave it at this stage.

If anyone has an idea on how to achieve this functionality without overriding WndProc or adding other API calls or hooks, please let me know.


  • 2004-12-04 - Initial release.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Robert Rohde
Web Developer
Germany Germany
No Biography provided

You may also be interested in...


Comments and Discussions

QuestionDoesn't work on windows 7 machine Pin
kishore11043-Oct-14 16:46
memberkishore11043-Oct-14 16:46 
SuggestionHow about default sorting by Pin
gochamax18-Sep-11 4:55
membergochamax18-Sep-11 4:55 
GeneralWindows 7 Pin
Sera019-Oct-09 21:12
memberSera019-Oct-09 21:12 
GeneralChangeing FileDlgFilter while running Pin
Member 45231945-Feb-09 1:56
memberMember 45231945-Feb-09 1:56 
GeneralRewritten for VB2005 Pin
Member 425774225-Jan-08 1:35
memberMember 425774225-Jan-08 1:35 
GeneralYou are my hero! Pin
Marakai29-Apr-07 17:03
memberMarakai29-Apr-07 17:03 
QuestionChange the size Pin
Esben Sundgaard20-Oct-06 0:58
memberEsben Sundgaard20-Oct-06 0:58 
AnswerRe: Change the size Pin
Robert Rohde20-Oct-06 21:41
memberRobert Rohde20-Oct-06 21:41 
GeneralRe: Change the size Pin
Esben Sundgaard21-Oct-06 10:02
memberEsben Sundgaard21-Oct-06 10:02 
Generalmultiple files open dialog box. Pin
jeevaggan21-Jun-06 20:32
memberjeevaggan21-Jun-06 20:32 
GeneralRe: multiple files open dialog box. Pin
Robert Rohde21-Jun-06 23:10
memberRobert Rohde21-Jun-06 23:10 
GeneralAnother implementation without WndProc Pin
stancrm24-Apr-06 3:27
memberstancrm24-Apr-06 3:27 
GeneralRe: Another implementation without WndProc Pin
Robert Rohde26-Apr-06 10:02
memberRobert Rohde26-Apr-06 10:02 
GeneralLike the code below you don't have to call StartWatching Pin
henrik_stromberg21-Sep-07 3:25
memberhenrik_stromberg21-Sep-07 3:25 
GeneralRe: Like the code below you don't have to call StartWatching Pin
tmackey2-Apr-09 11:06
membertmackey2-Apr-09 11:06 
GeneralRe: Like the code below you don't have to call StartWatching Pin
Graham Helliwell16-Apr-09 4:22
memberGraham Helliwell16-Apr-09 4:22 
GeneralRe: Like the code below you don't have to call StartWatching Pin
Dean Geddes15-Jul-09 5:03
memberDean Geddes15-Jul-09 5:03 
GeneralRe: Like the code below you don't have to call StartWatching Pin
Keith Watson13-Aug-09 12:50
memberKeith Watson13-Aug-09 12:50 
GeneralI agree ... you rock Pin
mikecline7-Mar-06 14:40
membermikecline7-Mar-06 14:40 
GeneralYou Rock Man [modified] Pin
LightingToGo1-Jan-06 13:32
memberLightingToGo1-Jan-06 13:32 
QuestionDose not Work In child form of MDI Form? Pin
majidbhutta14-Nov-05 5:47
membermajidbhutta14-Nov-05 5:47 
AnswerRe: Dose not Work In child form of MDI Form? Pin
Robert Rohde14-Nov-05 7:43
memberRobert Rohde14-Nov-05 7:43 
AnswerRe: Dose not Work In child form of MDI Form? Pin
Robert Rohde19-Nov-05 14:23
memberRobert Rohde19-Nov-05 14:23 
GeneralImplementation seems to be OS dependent Pin
CristianC107-Jul-05 5:41
sussCristianC107-Jul-05 5:41 
Generalcontact Pin
manatarms243-Jul-05 14:15
membermanatarms243-Jul-05 14:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.161018.1 | Last Updated 4 Dec 2004
Article Copyright 2004 by Robert Rohde
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid