Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.
Hi all, please see this major site announcement: https://www.boards.ie/discussion/2058427594/boards-ie-2026

C# saveDialog box

  • 22-03-2007 11:01PM
    #1
    Registered Users, Registered Users 2 Posts: 1,991 ✭✭✭


    I have the below code, it creates the file everytime i click save but i can't figure out how to pass the text from a textfield into the file when its saving!??
    private void cmdSave_Click(object sender, System.EventArgs e)
    {
    	SaveFileDialog sf = new SaveFileDialog();
    	sf.Filter = "Text Files (*.txt)|*.txt|" + "All Files|";
    	sf.ShowDialog();
    }
    


Comments

  • Registered Users, Registered Users 2 Posts: 541 ✭✭✭Vorrtexx


    The SaveFileDialog is used to specify a filename on where to save a file to.
    It's up to yourself to take that location specified and create the file there.

    ShowDialog() will return a DialogResult.OK when the Save button is clicked on the SaveFileDialog form. After this you need to write code to save the value in your textbox to that location.

    Something as follows for example:
    SaveFileDialog sf = new SaveFileDialog();
    sf.Filter = "Text Files (*.txt)|*.txt|" + "All Files|";
    if(sf.ShowDialog() == DialogResult.OK)
    {
    	System.IO.StreamWriter myFile = new System.IO.StreamWriter(sf.FileName);
    	myFile.Write(textBox1.Text);
    	myFile.Close();
    }
    

    Make sure to check for errors also and wrap the code in a try/catch in case of Permission errors or any other File IO exceptions that could occur.


  • Registered Users, Registered Users 2 Posts: 1,991 ✭✭✭Ziycon


    Nice one, got it sorted!


Advertisement