Saving to a file from a Textbox while preservice linebreaks.

This is a discussion on Saving to a file from a Textbox while preservice linebreaks. within the C# Programming forums, part of the General Programming Boards category; Hello, I am new to C# and Visual C#. I am writing an application that reads text from a file ...

  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 01:25 PM.

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    You don't seem to do anything with each line, so first splitting into lines then putting back to full text seems unneccessary. I suggest looking into System.IO.File.ReadAllText() and System.IO.File.WriteAllText(). If you're indeed interested in each line, look into System.IO.File.ReadAllLines() and System.IO.File.WriteAllLines().
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User
    Join Date
    Sep 2007
    Location
    Arizona
    Posts
    164
    I have been researching C# through Visual Studio all day. I have been working on this one little problem all day. I still have the meat of my program lef tto code, which is searching for the particular word and modifying the text of that word slightly and counting the word instances and then comparing them to the target number the user has identified. I thought this would be an easier part of my code. NOT! I hope the rest isn't as difficult as this seems to be otherwise I won't meet my submission deadline. ARG!

    I will take a look at the methods you posted and see if I can get this to work.

    I did this to myself. This was my program proposal for the class. I was an idiot!

    Thanks...

  4. #4
    Registered User
    Join Date
    Sep 2007
    Location
    Arizona
    Posts
    164
    I have been trying to get this but I am still having problems. My problem is that I am reading from the textBox control to a file. And, I don't seem to get how to make the proper conversion.

    I am getting a compile error: cannot convert type 'char' to type 'string.' And, when I click on the error it highlights the 'foreach' statement.
    I can't figure out what I have to do to get this to work.
    This is the code:
    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)
                {
                    string readText = targetDocument.ToString();
                    StreamWriter writer = new StreamWriter(saveFileDialog.FileName);
                    foreach (string s in readText)
                    {
                        writer.WriteLine(s);
                    }
                    writer.Close();
                    MessageBox.Show("THE FILE: \n\n'" + saveFileDialog.FileName + "'\n\nHAS BEEN SAVED!", saveFileDialog.Title);
    
                }
            }
    Thanks...
    I appreciate all the help.

  5. #5
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    The error indeed refers to the foreach. When you iterate each string you walk through each individual character which is a char, not a string.

    If you want to dump the contents of the textbox to file, do this:
    Code:
    System.IO.File.WriteAllText(saveFileDialog.FileName, targetDocument.Text);
    assuming targetDocument is the textbox.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  6. #6
    Registered User
    Join Date
    Sep 2007
    Location
    Arizona
    Posts
    164
    Thanks!

    I will rewrite my code to include that statement.

    I am having another huge issue with another method of this program, but it is something totally different. So, when I get a break at work, I will start a new thread.

    I can't find how to load an array from the textbox so I search each word to compare to a word entered but the user into another textbox. ARG!

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, 01: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21