我正在开发以xpdl格式导出数据。有两个选项 - 版本2.1和2.2。我正在使用SaveFileDialog,但我如何区分这两个选项?
SaveFileDialog dlg = new SaveFileDialog();
dlg.Filter = "xpdl 2.1|*.xpdl|xpdl 2.2|*.xpdl";
if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
//how can I check, which format is selected?
}
答案 0 :(得分:9)
您可以通过选中FilterIndex属性来获取或设置对话框的选定过滤器。并且如msdn所述:
第一个过滤条目的索引值为1.
所以你的任务就是:
SaveFileDialog dlg = new SaveFileDialog();
dlg.Filter = "xpdl 2.1|*.xpdl|xpdl 2.2|*.xpdl";
if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
switch (dlg.FilterIndex)
{
case 1:
//selected xpdl 2.1
break;
case 2:
//selected xpdl 2.2
break;
}
}
答案 1 :(得分:1)
拆分过滤器列表。然后查看FilterIndex。
SaveFileDialog dlg = new SaveFileDialog();
dlg.Filter = "xpdl 2.1|*.xpdl|xpdl 2.2|*.xpdl";
if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string[] filterstring = saveFilaDialog.Filter.Split('|');
MessageBox.Show(filterstring[(saveFilaDialog.FilterIndex - 1) * 2]);
}