Closed Thread
Results 1 to 6 of 6

Thread: Saving a textbox to User Defined File?

  1. #1
    Psynic's Avatar
    Psynic is offline Learning Programmer
    Join Date
    May 2009
    Location
    Canada, AB
    Posts
    61
    Blog Entries
    1
    Rep Power
    11

    Saving a textbox to User Defined File?

    There may be alot of questions coming out of me for my notepad project but im going to finish it dammit!

    right now i am trying to get my save feature functioning i have found several different examples on how to do it, but i am having trouble adapting it to my specific situation right now this is the code i have for the save feature.

    Code:
            private void saveToolStripMenuItem_Click(object sender, EventArgs e)
            {
                SaveFileDialog savDialog = new SaveFileDialog();
                savDialog.Title = "Specify Destination Filename";
                savDialog.Filter = "Text Files(*.txt)\"|*.txt";
                savDialog.FilterIndex = 1;
                savDialog.OverwritePrompt = true;
                
                FileStream fs = new FileStream(savDialog.FileName, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite);
                StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.ASCII);
                sw.Write(richTextBox1.Text);
                sw.Flush();
                sw.Close(); fs.Close();
            }
    i am getting an error on this line FileStream fs = new FileStream(savDialog.FileName, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite);
    it says that an empty path name is not legal when i use the save feature during debugging.

    can anyone shed some light on this? essentially my goal is to get the "Save As" form to open and have the user be able to name the file and save it in the directory they choose.

    richTextBox1 is the name of the textbox i am trying to save into a text file.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    QuackWare is offline Learning Programmer
    Join Date
    Jan 2010
    Posts
    95
    Rep Power
    8

    Re: Saving a textbox to User Defined File?

    Your not actually opening up the dialog, just giving the path before you even select it. Add this if statement which I bolded in your new code. It will show the dialog and also only execute the save code if the user clicks "OK".

    Code:
    SaveFileDialog savDialog = new SaveFileDialog();
    savDialog.Title = "Specify Destination Filename";
    savDialog.Filter = "Text Files(*.txt)\"|*.txt";
    savDialog.FilterIndex = 1;
    savDialog.OverwritePrompt = true;
    
    if (savDialog.ShowDialog() == DialogResult.OK)
    {
         FileStream fs = new FileStream(savDialog.FileName, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite);
         StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.ASCII);
         sw.Write(richTextBox1.Text);
         sw.Flush();
         sw.Close(); fs.Close();
    }

  4. #3
    Psynic's Avatar
    Psynic is offline Learning Programmer
    Join Date
    May 2009
    Location
    Canada, AB
    Posts
    61
    Blog Entries
    1
    Rep Power
    11

    Re: Saving a textbox to User Defined File?

    Such a small thing. thanks soooo much QuackWare!!! yay its saves.

  5. #4
    Psynic's Avatar
    Psynic is offline Learning Programmer
    Join Date
    May 2009
    Location
    Canada, AB
    Posts
    61
    Blog Entries
    1
    Rep Power
    11

    Re: Saving a textbox to User Defined File?

    You dont happen to know how i can get my textbox to scale with the window? its for making a notepad.

  6. #5
    QuackWare is offline Learning Programmer
    Join Date
    Jan 2010
    Posts
    95
    Rep Power
    8

    Re: Saving a textbox to User Defined File?

    I'm not in front of my computer right now but I think it has to do with the "anchor" property.

  7. #6
    lobo521 is offline Learning Programmer
    Join Date
    Jan 2010
    Posts
    57
    Rep Power
    8

    Re: Saving a textbox to User Defined File?

    You can also dock textbox in parent container.

    With IDisposable better use "using" keyword:

    Code:
    using (var fs = new FileStream(savDialog.FileName, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite))
    using (var sw = new StreamWriter(fs, Encoding.ASCII))
    	sw.Write(richTextBox1.Text);

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. User Defined Operators
    By AIGuy in forum Java Help
    Replies: 7
    Last Post: 10-02-2011, 07:02 PM
  2. Replies: 21
    Last Post: 06-11-2011, 07:46 PM
  3. some basic points about user-defined functions
    By jackson6612 in forum C and C++
    Replies: 2
    Last Post: 05-26-2011, 07:44 PM
  4. names of user defined functions
    By jackson6612 in forum C and C++
    Replies: 3
    Last Post: 05-14-2011, 03:07 PM
  5. user defined functions
    By akashdeep in forum PHP Development
    Replies: 1
    Last Post: 01-22-2010, 08:24 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts