open file dialog multislection order problem

I have a open dialog which need to oepn a set of files, but the problem is when i select multiple files, the files names are accumulated at the filename box with RANDOM order.  But for my program, i really need to get files in the order when i ctrl-click on them  because i have to read them in order. 

i tried to use   string[] filenames=of1.FileNames;

however, the filenames are in random order.

what should i do

many thanks

 



Answer this question

open file dialog multislection order problem

  • GazCoder

    Hi Danny:

    I just raised a similar issue with FileDialog and HookProc.

    OpenFileDialog is out of the question because this class is sealed where you cannot create a derived class.  However, the base class to OpenFileDialog is FileDialog and it is not sealed.

    Similar to my open issue, if you can create and implement a HookProc implementation for a class you derive from FileDialog, you can then track down the list box control and extend the defaut window style of the list box control with something like "style |=  LBS_SORT;".  This style option is built into windows for list boxes, so I do not think you would need anymore code.

    In your HookProc, you would need to trap on the WM_NCCREATE or WM_CREATE message, look for the list box control, and the update its style to automatically sort.  I think window style used to be set in the MFC within something like PreCreateWindow() which is similar to the WM_NCCREATE in the Win32 SDK.  WM_NCCREATE was designed to handle special data prior to creating a window or its child controls.  In VC++, resource files for dialogs could be updated and changed to compile with the LBS_SORT option.  I cannot recall if windows supports changing the style of a window after its creation and at runtime.  I am also trying to remember if the WM_CREATE message is given just before or just after the dialog window or controls are created or just after.  It may be before because modal dialog windows also have the WM_INITDIALOG message that is sent to initialize the dialog after all windows are created; this should all be handled by the FileDialog default base class functionality.

    protected override IntPtr HookProc (
    	IntPtr hWnd,
    	int msg,
    	IntPtr wparam,
    	IntPtr lparam
    )
    Steps Towards Solution
    • Derive a class from FileDialog. Create an override to the HookProc procedure. 
    • Locate the dialog window, get its id from comdlg32.dll (I think...).
    • Open the dll in Visual Studio and then look to the resources to find the your dialog of choice.
    • There is more than one version of the OPENFILENAME dialog available, maybe four in all.
    • Obtain the handle to the dialog window, maybe with FindWindow().
    • Obtain the handle to the list box control and then update the window style of the control.
    • After the message is processed, call the base class HookProc() or DefDlgProc() within your HookProc.
    • DefDlgProc() (spelling ) calls back to continue default processing for the dialog.
    • Note: It some cases, it is not necessary to continue default processing for the dialog.
    Summary
    • I have yet to implement a HookProc in C#. The use of IntPtr arguments might make thinks a bit tricky.
    • I am optimistic that you can accomplish this endeavor.
    • If you can implement this solution, it should be easier than my related problem.
    • History says that one cannot subtract any controls or functionality from the OPENFILENAME dialogs.
    • History also says that you can, though, add controls and/or functionality to OPENFILENAME dialogs.
    • VC# classes may help ease the default implementation process.
    • VC# classes may also limit or hinder functionality built into the Win32 API interface.
    • If successfull, all items in the dialog's list box will be sorted at all times.
    Let me know if you reach success or have any other questions on this issue.
    James Sigler, 10-24-2006
    Dallas, TX
    jmsigler2@hotmail.com
    P.S. I think you can change the style of a window after it is created because I seem to recall that
    a developer could add a check box control to the dialog window at runtime possibly to enable or
    disable sorting.
    P.S.S. In VC++, one trick I used to do was to copy the OPENFILENAME dialog from the comdlg32.dll
    into the resource file of my project. The resource file names are actually ids. Then, I could change
    properties on the controls at design time versus runtime. I could also add additional controls. However,
    I could not delete any controls provided for in the original dialog. By copying the dialog into
    my project as a resource, when the dialog was called with the same dialog id, my custom dialog would
    be called first. I have never tried this in VC#, so I do not know if it is supported. However, if it
    is, your solution might be solved very fast; without even the need for a HookProc. If it does work,
    remember, your dialog will override the default OPENFILENAME dialog for that application.

  • Luis Esteban Valencia Muñoz

    Before you use fileNames, you can manually sort them using Array.Sort() etc.

    I hope this will help!

    Best Regards,



  • BD_Elektrona

    This is not posible with the default OpenFileDialog. You should create your own dialog to provide this funtionality.You can read directories easy, everything you need for that can be found in the System.IO namespace.

    Please feel free to ask for any more information when you have problems with this solution!



  • marcoMB

    As PJ suggests, you should create your own dialog. It probably should have two listboxes, one initially empty; the other, with the list of potential files. As the user, double-clicks on a filename in the filled listbox, it should be copied to the other. And, you should have up/down arrows to reorder the file in the other listbox. (The concept is fairly common, but if you need to see an example, see the Customize Toolbar dialog in Windows Explorer (View\Toolbars\Customize...))

  • ShakyMobo

    Sorry for advert, but it seems this is possible using our Dialog Workshop .NET product. Our special CaOpenFileDialog/CaSaveFileDialog (powerful replacement of the standard Open/Save dialog components) contain SelectionChange event which is raised every time user changes selected files list in the dialog. So, comparing list of selected files before and after SelectionChange, you can build list of files in the order of Ctrl-clicks. Of course, you should preserve this list in your own structure and refresh this list on every SelectionChange event. Please visit http://www.componentage.com if you want to get more information.

    Best regards,
    Alex



  • open file dialog multislection order problem