Thread: Saving to a file from a Textbox while preservice linebreaks.

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Sep 2007
    Location
    Arizona
    Posts
    164

    Saving to a file from a Textbox while preserving linebreaks.

    Hello,

    I am new to C# and Visual C#. I am writing an application that reads text from a file into a textbox, then I will search the text for a particular to count the number of times that word exists, then save the modified text in a file. I was able to get the input into the textbox to work right. But, now I am working on saving the textbox text into a file. I was able to get this to work with the write method, but there weren't any line breaks. I need to preserve the line breaks.

    I have tried to modify the code to use the writeline method, but nothing I have used seems to work. I am trying to modify the code I have that loaded the text into the textbox from the opened file, but it just isn't working.

    Can anyone give me a suggestion on what I am missing?

    Here is the code I have for the saveFileMenu_Click that worked but didn't preserve the line breaks:
    Code:
     private void saveFileMenu_Click(object sender, RoutedEventArgs e)
     {
                saveFileDialog.InitialDirectory = System.Environment.GetFolderPath(Environment.SpecialFolder.Personal);
                saveFileDialog.FileName = targetWord.Text.ToUpper() + "_" + fileName.Text;
                saveFileDialog.DefaultExt = "txt";
                saveFileDialog.AddExtension = true;
                saveFileDialog.OverwritePrompt = true;
                saveFileDialog.Title = "Word Count Save As";
                saveFileDialog.ValidateNames = true;
                if (saveFileDialog.ShowDialog().Value)
                {
                    StreamWriter writer = new StreamWriter(saveFileDialog.FileName);
                    writer.Write(targetDocument.Text);
                    writer.Close();
                    MessageBox.Show("THE FILE: \n\n'" + saveFileDialog.FileName + "'\n\nHAS BEEN SAVED!", saveFileDialog.Title);
                }
    }
    This is what I am trying to mimic from the openFileDialog_Click which loads the textbox, but I can't get it to work:
    Code:
    targetDocument.Text = "";
    TextReader reader = source.OpenText();
    string rLine = reader.ReadLine();
    while (rLine != null)
    {
        targetDocument.Text += rLine + "\n";
        rLine = reader.ReadLine();
    }
    reader.Close();
    But it just isn't coming together.
    I would appreciate a little electricity for the light bulb that is over my head that isn't as bright as it should be. (My attempt at a little stupid humor!).

    Thanks...
    Last edited by clegs; 03-01-2009 at 02:25 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM