How to check which filter is applied

I am developing xpdl data export. There are 2 options - versions 2.1 and 2.2. I use SaveFileDialog, but how can I distinguish between these 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? } 
+6
source share
2 answers

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

The index value for the first filter entry is 1.

So, for your task this will 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; } } 
+7
source

Break the list of filters. Then look at 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]); } 
+1
source

Source: https://habr.com/ru/post/922903/