views:

649

answers:

3

We have an SaveFileDialog in our application, which offers a variety of formats the user can export media in. We determine the user's choice of format using the FilterIndex property of the SaveFileDialog. The various formats have different file extensions, so we would like the file name that the user has entered to change extension when the user changes the selected filter. Is this possible, and if so, how?

EDIT: I want this to happen while the dialog is shown, when the user changes the filter, so the user gets feedback on what the filename will be, rather than afterwards when the user closes the dialog. I've tried using a message filter, but it doesn't receive messages for the dialog. I've tried Application.Idle but that never fires while the dialog is running. I've tried a background thread, but FilterIndex doesn't get updated until the user closes the dialog.

A: 

SaveFileDialog changes extension of the file automatically when user changes filter. If you want to process some certain actions for different file formats you can youse something like this:

...
if (saveDialog.ShowDialog() == DialogResult.OK)
{
    switch (saveDialog.FilterIndex)
    { 
     case 0:
      ...
      break;
     case 1:
      ...
      break;
     default:
      ...
      break;
    }
}
...
iburlakov
SaveFileDialog doesn't change the extension. I've checked.
Simon
It changes the extension. Trust me.
Sani Huttunen
It's a little impractical though, because then this must be kept in-line with the Filter of the SaveFileDialog, if somebody comes along and adds an extension to the front of the filter, they then have to come along and find this code to change also.
ThePower
True. But then you'd use something like dictionary to store the extensions and only change the dictionary.
Sani Huttunen
Someone in the comments points out that I may have been unclear. It doesn't change the extension while the user can see the form, only afterwards. I need it to change while the form is shown.
Simon
A: 

Add your filters:

saveFileDialog1.Filter = "txt files (*.txt)|*.txt|Word files (*.doc)|*.doc";

then:

if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
  switch (saveFileDialog1.FilterIndex)
  {
    case 1:
      saveFileDialog1.FileName = System.IO.Path.ChangeExtension(saveFileDialog1.FileName, "txt");
      break;
    case 2:
      saveFileDialog1.FileName = System.IO.Path.ChangeExtension(saveFileDialog1.FileName, "doc");
      break;
  }
  // Here you would save your file with the filename in saveFileDialog1.FileName.
  MessageBox.Show(saveFileDialog1.FileName);
}

Runt it twice, first select "txt files" then "Word files". Enter "test" as the filename.
You will see that the filename is different in both cases: text.txt and test.doc.

If you enter a filename with an extension like "test.htm" then the extension is changed when you switch filter.

If you enter a filename like "test.htm" and DON'T change the filter the switch case takes care of the extension for you.

Sani Huttunen
The SaveFileDialog does change the extension if he already has entered an extension. IF he switches filter. If he doesn't the extension isn't changed but as Ivan Burklov suggested he can check the FilterIndex and then change it manually.
Sani Huttunen
The file isn't saved until he saves it. He can process the filename anyway he wants to before he saves it.
Sani Huttunen
Now you've changed the code it does yes
ThePower
No code is changed... Just added the switch statement like Ivan Burklov suggested.
Sani Huttunen
+1  A: 

As SaveFileDialog can't be inherited, I guess you must build your own, using FileDialog as the base class.

rockeye