Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change file extension when user changes Save As Type in SaveFileDialog

I have a SaveFileDialog with the option of saving with type .foo or .bar. The first item in the list, and selected by default, is .foo. The default filename is "untitled", and the default extension is ".foo". When the SaveFileDialog appears, it puts "untitled" in the file name textbox. I can change it to "untitled.foo" but it still doesn't change the behavior in regards to my problem:

If the user switches to .bar, how can I make the filename change to untitled.bar? There's only two events, neither of which is the one I want, and it doesn't seem to be changing itself.

like image 419
Ed Marty Avatar asked Nov 08 '10 22:11

Ed Marty


2 Answers

Ed,
I just tested and it works just fine.
I did this:

        SaveFileDialog sfd = new SaveFileDialog();

        sfd.FileName = "untitled";
        sfd.Filter = "Text (*.txt)|*.txt|Word Doc (*.doc)|*.doc";
        sfd.ShowDialog();

And it automatically changes the suggested save name depending on the filter I choose.
I used the .NET 2.0 framework.
But I'm on Windows 7, which I think matters, since you see the system's file-save dialog, and the way it's implemented is what matters here.

like image 158
BeemerGuy Avatar answered Oct 28 '22 01:10

BeemerGuy


Adding DefaultExt and AddExtension will give you the behaviour you're looking for. Simialr to question/answer provided here: https://stackoverflow.com/a/1213353/101971

        var saveFileDialog = new SaveFileDialog
                                 {
                                     Filter = "Foo (*.foo)|*.foo|Bar (*.bar)|*.bar",
                                     DefaultExt = "foo",
                                     AddExtension = true
                                 };
like image 27
MEF2A Avatar answered Oct 28 '22 00:10

MEF2A