Hi, I have added a SaveFileDialog to my project, to enable users to choose where they would like to save a txt file.

I am using the Filter property to enable saving the file as a txt document, but when I run the project and the SaveFileDialog appears, the 'save as type' is blank. How can I get it to show .txt in here?

this is what I am using in the properties of the SaveFileDialog I have added to my project:

Filter ---- "txt files (*.txt)|*.txt"

Do it this way:

SaveFileDialog saveFileDialog1 = new SaveFileDialog();

            saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
            saveFileDialog1.FilterIndex = 2;
            saveFileDialog1.RestoreDirectory = true;
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                //save file using textWriter object!
            }

It is still showing me nothing in the save as type!! ahhhhhhhhhh so annoying!!

Would you mind showing me your code? This way I`ll better see what is wrong.
thx

Sure...this is my method to save data:

public void saveData(QuestionForm ob)//write out users details at end of test
           {
               DialogResult ques = MessageBox.Show("Would you like to save you data?", "Save?", MessageBoxButtons.YesNo, MessageBoxIcon.Information);

               if (ques == DialogResult.Yes)
               {
                   ob.saveUserDetail = new SaveFileDialog();
                   ob.saveUserDetail.ShowDialog();
                   ob.saveUserDetail.FilterIndex = 2;
                   ob.saveUserDetail.RestoreDirectory = true;

               }
}

You don't appear to be setting the filter property as Mitja showed you. Is this all the code?

Hi, thanks for your response. I have already set the filter property on the SaveFileDialog object that I have added to my form?

The property on this object is set to:

"txt files (*.txt)|*.txt"

Look, why dont you do as I showed you? It will work 100%.

LOL Thanks Mitja, it works. Although out of interest I would like to know why I cannot set the filter property in a SaveFileDialog object that I add to my form?!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.