How to check what filter is applied
Asked Answered
T

2

9

I am developing exporting data in xpdl format. There are 2 options - version 2.1 and 2.2. I am using SaveFileDialog, but how can I distinguish between those 2 options?

        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?
        }
Thor answered 15/8, 2012 at 6:28 Comment(0)
S
12

You can get or set selected filter for dialogs by checking FilterIndex property. And as stated in msdn:

The index value of the first filter entry is 1.

So for your task it would be:

        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;
            }
        }
Starla answered 15/8, 2012 at 6:29 Comment(0)
I
2

Split the Filter list. Then look at the 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]);
}
Ivar answered 2/9, 2014 at 8:23 Comment(1)
Great answer sarathi... I have over 30 filters so it would be a pain to have to do a case statement.Triserial

© 2022 - 2024 — McMap. All rights reserved.