I want to have an custom OpenFileDialog form within my project where I can add/remove buttons and customize whatever I want like a normal form. This is mainly so that it fits into the theme I am using, additionally, I can add custom buttons. Is there any tutorials on how I could construct my own? Are there any pre-existing projects I can use straight out of the download?
Asked
Active
Viewed 6,904 times
3
-
1There's no straightforward way of doing this as `OpenFileDialog` is `sealed`. Possible duplicate http://stackoverflow.com/questions/7831432/how-to-create-customized-open-file-dialog-in-c-sharp – keyboardP Sep 27 '13 at 11:42
-
I know that it is sealed, no way to inherit it. I am talking about construction of one in a new form from scratch. – Trontor Sep 27 '13 at 11:43
-
1There are some options: http://stackoverflow.com/questions/6093012/customizing-openfiledialog -> http://www.codeproject.com/Articles/16276/Customizing-OpenFileDialog-in-NET – Daniel Abou Chleih Sep 27 '13 at 11:43
-
@Daniel I stated I want a custom _form_ in my project, not the standard OFD. – Trontor Sep 27 '13 at 11:44
-
1Check this: http://stackoverflow.com/questions/6093012/customizing-openfiledialog – user743414 Sep 27 '13 at 11:46
-
In that case a combination of `ListView` and `TreeView` may be what you want http://msdn.microsoft.com/en-us/library/ms171645.aspx. Other options here http://stackoverflow.com/questions/2416963/how-to-create-an-explorer-like-folder-browser-control – keyboardP Sep 27 '13 at 11:47
-
http://www.codeproject.com/Articles/19566/Extend-OpenFileDialog-and-SaveFileDialog-the-easy might help – Pradip Sep 27 '13 at 11:48
-
Ok, so you are using your own form. You could easily achieve that with a TreeView and Directory.GetFiles()/.GetDirectories() – Daniel Abou Chleih Sep 27 '13 at 11:48
-
Downvoted - please do some research before asking. A google search for "Winforms Custom OpenFileDialog" led me to another SO question, where the answers also pointed to the CodeProject-page about the topic. – Lars Kristensen Sep 27 '13 at 11:49
-
Woah, just did the same, I realise now how ignorant I was. Sorry. – Trontor Sep 27 '13 at 11:50
-
@PradipKT The project just EXTENDS onto the OFD. Which is not what I want. – Trontor Sep 27 '13 at 12:06
-
So you want to create your own Form with the ability to select a file. Am I right? – Daniel Abou Chleih Sep 27 '13 at 12:34
-
@DanielAbouChleih Spot on. – Trontor Sep 28 '13 at 00:15
2 Answers
13
I hope that will fit your requirements:
You will need one TreeView and an ImageList
Code
You will need System.Runtime.InteropServices;
And following code to get the associated icon from the path:
[StructLayout(LayoutKind.Sequential)]
public struct SHFILEINFO
{
public IntPtr hIcon;
public IntPtr iIcon;
public uint dwAttributes;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
public string szDisplayName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
public string szTypeName;
};
class Win32
{
public const uint SHGFI_ICON = 0x100;
public const uint SHGFI_LARGEICON = 0x0; // 'Large icon
public const uint SHGFI_SMALLICON = 0x1; // 'Small icon
[DllImport("shell32.dll")]
public static extern IntPtr SHGetFileInfo(string pszPath,
uint dwFileAttributes,
ref SHFILEINFO psfi,
uint cbSizeFileInfo,
uint uFlags);
}
private int GetIconOfFile_Folder(string Path)
{
IntPtr hImgSmall; //the handle to the system image list
IntPtr hImgLarge; //the handle to the system image list
string fName; // 'the file name to get icon from
SHFILEINFO shinfo = new SHFILEINFO();
//Use this to get the small Icon
hImgSmall = Win32.SHGetFileInfo(Path, 0, ref shinfo,
(uint)Marshal.SizeOf(shinfo),
Win32.SHGFI_ICON |
Win32.SHGFI_SMALLICON);
//Use this to get the large Icon
//hImgLarge = SHGetFileInfo(fName, 0,
//ref shinfo, (uint)Marshal.SizeOf(shinfo),
//Win32.SHGFI_ICON | Win32.SHGFI_LARGEICON);
//The icon is returned in the hIcon member of the shinfo
//struct
System.Drawing.Icon myIcon =
System.Drawing.Icon.FromHandle(shinfo.hIcon);
imageList1.Images.Add(myIcon);
return imageList1.Images.Count - 1;
}
Use following Method to Get all your Drives (best place it in your constructor/Form_Load):
private void GetAllDrives()
{
DriveInfo[] drives = DriveInfo.GetDrives();
foreach (var drive in drives)
{
TreeNode rootTreeNode = new TreeNode();
rootTreeNode.Text = drive.Name;
rootTreeNode.Tag = drive.Name;
rootTreeNode.ImageIndex = GetIconOfFile_Folder(drive.Name);
rootTreeNode.SelectedImageIndex = rootTreeNode.ImageIndex;
rootTreeNode.Nodes.Add(" "); //Placeholder to enable expanding (+)
treeView1.Nodes.Add(rootTreeNode);
}
}
Then you will need an EventHandler for the Expand-Event, which will call the method GetFilesAndFolder()
private void treeView1_BeforeExpand(object sender, TreeViewCancelEventArgs e)
{
e.Node.Nodes.Clear();
GetFilesAndFolder(e.Node, (string)e.Node.Tag);
}
private void GetFilesAndFolder(TreeNode tn, string Path)
{
try
{
string[] Directories = Directory.GetDirectories(Path);
string[] Files = Directory.GetFiles(Path);
foreach (string dir in Directories)
{
TreeNode dirTreeNode = new TreeNode();
dirTreeNode.Tag = dir;
dirTreeNode.Text = new DirectoryInfo(dir).Name;
dirTreeNode.ImageIndex = GetIconOfFile_Folder(dir);
dirTreeNode.SelectedImageIndex = dirTreeNode.ImageIndex;
dirTreeNode.Nodes.Add(" ");
tn.Nodes.Add(dirTreeNode);
}
foreach (string file in Files)
{
TreeNode fileTreeNode = new TreeNode();
fileTreeNode.Tag = file;
fileTreeNode.Text = new FileInfo(file).Name;
fileTreeNode.ImageIndex = GetIconOfFile_Folder(file);
fileTreeNode.SelectedImageIndex = fileTreeNode.ImageIndex;
tn.Nodes.Add(fileTreeNode);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, ex.Source, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
Finally I have created an EventHandler for the NodeDoubleClick-Event in the TreeView:
private void treeView1_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
{
if (CheckIfPathIsFile(e.Node.Tag.ToString()) == true) //If the Tag (Path) is a File
{
//Do something with the Path (close this Form + return Path)
}
}
private bool CheckIfPathIsFile(string Path)
{
// get the file attributes for file or directory
FileAttributes attr = File.GetAttributes(Path);
//detect whether its a directory or file
if ((attr & FileAttributes.Directory) == FileAttributes.Directory)
return false;
else
return true;
}

Daniel Abou Chleih
- 2,440
- 2
- 19
- 31
-
Thanks, exactly what I was looking for. I already solved my problem, but I shall mark it as the answer anyways ;D – Trontor Sep 28 '13 at 07:23
-
4
If you used WinForms, chances are that at some point you wanted to extend the OpenFileDialog or SaveFileDialog, but you gave up because there is no easy way to do it,Go with following link to get undestand how to custermize with your own... HERE

Thilina H
- 5,754
- 6
- 26
- 56
-
He does not want to extend it. Please read the comments before posting an answer – Daniel Abou Chleih Sep 28 '13 at 17:23
-
1FYI - tried using the referenced CodeProject "Extend OpenFileDialog and SaveFileDialog the easy way" today (4/4/2018) and neither the demo nor the source code works correctly on a Win 7 machine. The dialog displays ok, but the extra bits don't display correctly. I assume the standard open file dialog has changed to the point where the customizations just don't fly anymore – VA systems engineer Apr 04 '18 at 15:42
-
1@DanielAbouChleih the solution from Thilina H has a lot of costumizations, I have used and it works great, IMHO it is better than a "100% custom" open file dialog. – Luis Apr 17 '18 at 23:03
-
1@NovaSysEng The solution works, try https://github.com/dmihailescu/CustomFileDialog instead the old link – Luis Apr 17 '18 at 23:03