我正在使用C#中的winforms应用程序。我想要实现的是从用户那里获取一个文件,我正在使用以下代码:
OpenFileDialog dlg = new OpenFileDialog();
if (dlg.ShowDialog() == DialogResult.OK)
{
string sFileName = dlg.FileName;
//my code goes here
}
现在,一切正常,但我想在同一个对话框中放入3个单选按钮,这意味着我现在可以从这个对话框中得到两个东西
string sFileName = dlg.FileName; //same as in case of traditional dialog box
//some thing like this which tells which radio button is selected:
dlg.rbTypes.Selected
我如何实现这一目标?
答案 0 :(得分:9)
是的,这是可能的,我成功地使用SaveFileDialog
进行了同样的自定义,这非常有趣。
请点击以下链接:
http://www.codeproject.com/KB/dialog/OpenFileDialogEx.aspx
http://www.codeproject.com/KB/cs/getsavefilename.aspx
http://www.codeproject.com/KB/dialog/CustomizeFileDialog.aspx
我自己的问题也会对你有所帮助:
Change default arrangement of Save and Cancel buttons in SaveFileDialog
How to stop overwriteprompt when creating SaveFileDialog using GetSaveFileName
你必须使用WinAPI
,你需要在自己的ShowDialog
窗口函数中编写GetOpenFileName
方法,而不是调用.net的{{1} }}。 OpenFileDialog
将创建窗口GetOpenFileName
。 (请参阅http://msdn.microsoft.com/en-us/library/ms646927%28v=vs.85%29.aspx)。这与编写HookProc过程以及在其中捕获OpenFileDialog
等事件一起将帮助您做您想做的事。
要添加单选按钮等,您必须调用WM_INITDIALOG, CDN_INITDONE
和CreateWindowEx
等窗口函数。
第二个链接具有自定义的确切方向......
要求任何澄清......
答案 1 :(得分:3)
在XP上,您需要使用钩子过程方法和GetOpenFileName API。在Vista及更高版本中,这将导致具有有限效用的可怕外观文件对话框,例如,没有搜索。在Vista上,你应该使用IFileDialog并自定义你需要IFileDialogCustomize接口的对话框。因为新的Vista对话框是作为COM接口公开的,所以它们很容易在.net中使用。
答案 2 :(得分:-7)
试试这段代码:
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;
}
}