Customizing OpenFileDialog

不羁的心 提交于 2019-11-27 04:52:36
Dulini Atapattu

Yes, that's possible, I did the same kind of customization with SaveFileDialog successfully and it's pretty interesting.

Follow the following links:

http://www.codeproject.com/KB/dialog/OpenFileDialogEx.aspx

http://www.codeproject.com/KB/cs/getsavefilename.aspx

http://www.codeproject.com/KB/dialog/CustomizeFileDialog.aspx

Also my own questions too will help you:

Change default arrangement of Save and Cancel buttons in SaveFileDialog

How to stop overwriteprompt when creating SaveFileDialog using GetSaveFileName

You have to use the WinAPI for this and you need to write the ShowDialog method in your own calling the GetOpenFileName windows function inside it, instead of calling .net's OpenFileDialog. The GetOpenFileName will create the windows OpenFileDialog. (Refer to http://msdn.microsoft.com/en-us/library/ms646927%28v=vs.85%29.aspx). This together with writing the HookProc procedure and catching events such as WM_INITDIALOG, CDN_INITDONE inside it will help you do what you want.

To add radio buttons etc., you have to call the windows functions such as CreateWindowEx and SendMessage....

The 2nd link has the exact direction to the customization...

Ask for any clarifications...

On XP you need to use the hook procedure method and the GetOpenFileName API. On Vista and later this will result in a horrid looking file dialog with limited utility, e.g. no search. On Vista you should use IFileDialog and to customise the dialog you need the IFileDialogCustomize interface. Because the new Vista dialogs are exposed as COM interfaces they are quite easy to consume in .net.

Brajaraj Jena

Try this code:

private void Browse_Click(object sender, EventArgs e)
{
    var fdlg = new OpenFileDialog();
    fdlg.Title = "Open a file";
    fdlg.InitialDirectory = "c:/";
    fdlg.Filter = "all files(*.*)|*.*|all files(*.)|*.*";
    fdlg.FilterIndex = 2;
    fdlg.RestoreDirectory = true;
    if (fdlg.ShowDialog() == DialogResult.OK)
    {
        filetxt.Text = fdlg.FileName;
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!