Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

C# saveDialog box

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


    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,987 ✭✭✭Ziycon


    Nice one, got it sorted!


Advertisement