OpenFileDialog - how to select files by coding???

G

Guest

I’m using OpenFileDialog class in a specific InitialDirectory and I want that
files will be selected in the opened dialog when I call ShowDialog().
Actually I wish to select all files in the directory automatically by code.

Is it doable?
How??
 
N

Nicholas Paldino [.NET/C# MVP]

Sharon,

I don't believe that this is possible. You might be able to hack it by
getting the window handle for the file dialog and then getting the handle
for the listview, and then sending the message to select every item, but
that would seem excessive.

Does the possibility exist that only some files in the directory would
be selected, or will they always be selecting all of the files in the
directory? If it is the latter, then you might want to consider using a
DirectoryBrowseDialog (I believe that is what it is called), as it will let
you select directories.

Hope this helps.
 
G

Guest

Eventually I'm using the native API FindWindow() ans SendMessge(), but I can
find what message to send to the Form.ListBox.
Can anybody tell me how to complete and fix the following call so the
ListBox will select all files in it?

SendMessage(listviewHandle, 0x0111, ...

I already obtained the listviewHandle but all other parameters are missing
for me, especially I need the actual value of the message, like 0x702e for
Tiles view.
 
G

Guest

Ok, I have the answer:

[DllImport("user32.dll", EntryPoint="SendMessageA",
CallingConvention=CallingConvention.StdCall,CharSet=CharSet.Ansi)]
private static extern uint SendMessage(uint Hdc, uint Msg_Const, uint
wParam, uint lParam);

[DllImport("user32.dll", EntryPoint="FindWindowExA",
CallingConvention=CallingConvention.StdCall,CharSet=CharSet.Ansi)]
private static extern uint FindWindowEx(uint hwndParent, uint
hwndChildAfter, string lpszClass, string lpszWindow);

protected override void WndProc(ref Message m)
{
// Leting the Form take care off all messages.
base.WndProc(ref m);

if( m.Msg == 289 ) // Notify of message loop
{
uint dialogHandle = (uint)m.LParam; // Handle of the file dialog

if( dialogHandle != m_lastDialogHandle ) // Only when not already changed
{
// Getting the handle of the ListBox in the OpenFileDialog dialog.
uint listviewHandle = FindWindowEx(dialogHandle, 0,
"SHELLDLL_DefView", "");

// Sending message to the ListBox to set the view to Thumbnails
//Icons=0x7029, List=0x702b, Details=0x702c, Thumbnails=0x702d,
Tiles=0x702e
SendMessage(listviewHandle, 0x0111/*WM_COMMAND*/, (uint)0x702d, 0);

// Sending message to the ListBox to select all items.
SendMessage(listviewHandle, 0x0111/*WM_COMMAND*/, (uint)0x00017021,
(uint)0);

// Remember last handle
m_lastDialogHandle = dialogHandle;
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top