none
carriage return in a textbox.

    Question

  • I have a text box that has the AcceptReturns property to true. I am trying to display a text file in this text box, one line at a time with the following code:

    string line;

    this.textBoxSettingsFile.Text = "";

    // Read and display lines from the file until the end of

    // the file is reached.

    while ((line = sr.ReadLine()) != null)

    {

    this.textBoxSettingsFile.Text = this.textBoxSettingsFile.Text + line + Convert.ToChar(13);

    }

    This displays the file with a nonprintable character at the end of each line in the file.

    How do I programmatically send a carriage return to a text box?

    Thanks

    Thursday, March 23, 2006 5:17 PM

Answers

  • you can do as follows:

    while ((line = sr.ReadLine()) != null)
    {
    this
    .textBoxSettingsFile.Text = this.textBoxSettingsFile.Text + line + Environment.NewLine;
    }

    or

    while ((line = sr.ReadLine()) != null)
    {
    this
    .textBoxSettingsFile.Text = this.textBoxSettingsFile.Text + line + "\r\n";
    }


    hope this helps, regards.


    Thursday, March 23, 2006 5:43 PM

All replies

  • you can do as follows:

    while ((line = sr.ReadLine()) != null)
    {
    this
    .textBoxSettingsFile.Text = this.textBoxSettingsFile.Text + line + Environment.NewLine;
    }

    or

    while ((line = sr.ReadLine()) != null)
    {
    this
    .textBoxSettingsFile.Text = this.textBoxSettingsFile.Text + line + "\r\n";
    }


    hope this helps, regards.


    Thursday, March 23, 2006 5:43 PM
  • that did it. Thank you very much.
    Thursday, March 23, 2006 6:01 PM
  • remember to mark my post as useful ;)

    cya
    Thursday, March 23, 2006 6:25 PM
  • Solarin,

     

    Is possible to simulate just the Carriage Return "\r" like Windows Hyper Terminal?

     

    ie:

    if a have: string test =  "1111111\r222222222";

    textBox.text = test;

    I want see just: "222222222";

     

    Can you help me??

     

    Tks

     

    Sorry about my english!!

    Tuesday, May 27, 2008 7:04 PM
  •  Eder F. Dias wrote:

    Solarin,

     

    Is possible to simulate just the Carriage Return "\r" like Windows Hyper Terminal?

     

    ie:

    if a have: string test =  "1111111\r222222222";

    textBox.text = test;

    I want see just: "222222222";

     

    Can you help me??

     

    Tks

     

    Sorry about my english!!

     

    You can add any character to your text, like .textBox1.Text + "\\r";

     

    or

     

    textBox.Text + @"\r"; or textBox.Text + @"\n";

     

    or textBox.Text + Environment.NewLine; // this is equivalent to @"\r\n\"

     

    Tuesday, May 27, 2008 7:51 PM
  • I think Solarin may be meaning something other than what you are explaining AlexBB.

    In HyperTerminal, a \r by itself would advance the cursor down one row while leaving the column unchanged, hence the typical need for the \n also. The terminal emulator would have implimented this functional way of handling \r and \n and it is not the same in a textbox. If memory serves me, a \r by itself when printed will show as an unprintable character since windows encoding expects the \r\n (<CR><LF>) sequence.

    Your request to only see "22222222222" is a bit confusing as the only thing I can figure is you have a textbox tall enough to display only one line at a time?

    If you are dealing with data that contains \r and this is the issue, you can always to a .Replace() to convert them to \r\n before  displaying.

    Also, the above code is not entirely accurate.

    textBox.Text = Environment.NewLine; (I assume you mean = as you have done +, or at least +=)
    This is not the equivilent of @"\r\n\", this is the equivilent of "\r\n".

    textBox.Text = "\\r\\n\\" is the equivilent of @"\r\n\" since @ does not interpret escape sequences.


    -Vaelek
    The Coding Hut
    Tuesday, May 27, 2008 11:06 PM
  •  Vaelek wrote:
    I think Solarin may be meaning something other than what you are explaining AlexBB.

    In HyperTerminal, a \r by itself would advance the cursor down one row while leaving the column unchanged, hence the typical need for the \n also. The terminal emulator would have implimented this functional way of handling \r and \n and it is not the same in a textbox. If memory serves me, a \r by itself when printed will show as an unprintable character since windows encoding expects the \r\n (<CR><LF>) sequence.

    Your request to only see "22222222222" is a bit confusing as the only thing I can figure is you have a textbox tall enough to display only one line at a time?

    If you are dealing with data that contains \r and this is the issue, you can always to a .Replace() to convert them to \r\n before  displaying.

    Also, the above code is not entirely accurate.

    textBox.Text = Environment.NewLine; (I assume you mean = as you have done +, or at least +=)
    This is not the equivilent of @"\r\n\", this is the equivilent of "\r\n".

    textBox.Text = "\\r\\n\\" is the equivilent of @"\r\n\" since @ does not interpret escape sequences.


    -Vaelek
    The Coding Hut

     

    I don't know if you are talking to me in that highlighted clause, if you do, please be informed that you are mistaken. I've never talked about any "2222222222" or anything like this. Honestly, I did not even read the post at all just grasped what the other poster said about the line terminating characters and decided to offer more options.

     

    You are equally mistaken about

     

    textBox.Text = Environment.NewLine;

     

    Please reread my post. Reread it twice. What I wrote was:

     

    textBox.Text + Environment.NewLine;

     

    It means that when youi are adding a text to a tetxBox you can add this Environment.NewLine set of two characters at the end of it as follows:

     

    this.textbox1.Text = this.TextBox.Text + Environment.NewLine;

     

    or

     

    this.textbox1.Text += Environment.NewLine;

     

    or

     

    this.textbox1.Text += sometext + Environment.NewLine;

     

    It's been years since I worked with Hyperterminal but in textBox you can use a NewLine set oc characters but I am not sure if they both are encoded. I am absolutely certain that only ONE character of the pair ends up in the actual text. That character is "\n"

     

    Thus. there is nothing wrong with adding "\r\n" to the end of any text to terminate the input in a textbox but you will end up having only ONE character as a result.

     

    If you attribute this to me: textBox.Text = \\r\\n\\ again, reread my post more carefully.

     

    I just wanted to demonstrate that there are a number of options, like using "\n" "\r", "r\n" - the latter is the case for the line terminals in csv files, or Environment.NewLine for this matter.

     

     

     

     

    Wednesday, May 28, 2008 12:04 AM
  •  

    AlexBB...  Thanks for reply...

     

    What I really need is the same behavior of Carriage Return (not "\r\n" ) of the HyperTerminal in TextBox.

     

    There is another component that simulates this behavior?

     

    Thanks to all....

     

     

     

     

     

    Wednesday, May 28, 2008 12:54 PM
  •  Eder F. Dias wrote:

     

    AlexBB...  Thanks for reply...

     

    What I really need is the same behavior of Carriage Return (not "\r\n" ) of the HyperTerminal in TextBox.

     

    There is another component that simulates this behavior?

     

    Thanks to all....

     

    I did not quite understand your question. Most likely you will have to experiment based on the information provided because it is not clear what you want. let me guess though.

     

    You perhaps want to enter programmatically into you text an equivalent of CR? Right? Then enter \n. Just add sometext += @"\n";

     

    You will be as well with adding both characters: someText += "\r\n"; or someText += Environmen.tNewLine; - this is what I do.

     

    HOWEVER, if you want to parse your text pulled from textbox (or richTextBox) you will find only one character in there, and this character is \n.

     

    What do you mean by simulating the behavior of Hyperterminal? You should describe it in detail. What behavior?

    Wednesday, May 28, 2008 1:09 PM
  • AlexBB,

     

    I had a rather lengthy response for you but the forums went down and I do not care to retype it. I must say, your desire to interchange "xx" and @"xx" is rather disturbing for lack of a better word.

     

    In no way shape or form would sometext += @"\n" accomplish anything that is desired here.

    @"\n" is the same as actually typing \n into the textbox.

     

    sometext += @"\n" IS the equivalent of sometext += "\ \n" in that the character "\" WILL be included in the actual string along with the letter n EXACTLY as it appears in the quotes, which is the entire purpose of prefixing a string with @ and has no application at all to Eder's question. (ignore the space, this editor keeps turning it into a link without it)

     

    Your previous comment about being absolutely certain that only one character ends up in the text for a line break is also just false. Test it yourself. Create 2 textboxes and a function to populate the second with the character codes from the first with a simple foreach. type something into textbox1, hit enter, type something else, run the function. you will see clear as day 10-13 which you guessed it is \r\n. I will say that if you specifically use "\n" by itself, 13 is the only character that will end up in the final string.

     

    I in no way want to start a flame war or anything else here. Guesses are one thing when you're trying to help someone, but giving bad information is just not cool but actually worse than saying nothing at all.

     

    With that said, I do agree with your last statement. Eder, please describe exactly what you are trying to acheive. Where is your data coming from? What are you assigning to the textbox, and how exactly do you want it to appear?

     

    -Vaelek

    Wednesday, May 28, 2008 11:04 PM
  •  Test it yourself. Create 2 textboxes

     

    I test it myself daily.Or rather i did about 3 months ago while working with an extended RichTextBox by David Bennet. ExRichTextBox inherits from RichTextBox and the latter in turn from the TextBox. I coded a speller that does the spelling on the fly. I needed to analyze every character.

     

    I type text myself using CR and invariably I end up having only one LF character in the text. If I hit CR twice I find two of them glued together. I see them in Debug when I set up local variables to ogle. I have never seen char ( 13 ).

     

    There was a thread recently whereas a person was asking a basic question just how to get his text out of Textbox. We explained to him that he should set up an event delegate (_textChanged) and then set up a filter to catch the CR. I suggested this code:

     

    if ( this.textbox.text.IndexOf ( Environment.NewLine) != -1 )

    {

    // send it to Sql Server.

    }

     

    He came back saying it did not work. It immediately occured to me that only one character ("\n") was actually embedded in the code because of my prior experience.

     

    That worked.

     

    You don't have to use your condescending explanation (you repeated it twice as if you are not quite sure about it yourself) that @"\r" == "\\r". 

     

    I've written perhaps a quarter million lines of code in C#. I don't need it.

     

    My understanding is that the OP had a need to catch a text termination event. He can do it only with @"\n" in textbox. Period.

     

    Most likely he has already done it. He does not care about this thread anymore othewise he would have come back asking more questions. I remember my first weeks in C# when you feel like you are climbing a wall of the Grand Canyon. He has no interest in anything but making his code run until the next place where it breaks down five minutes later.

    Wednesday, May 28, 2008 11:21 PM
  • AlexBB, I'm sorry but regardless of your previous project, you are still incorrect about \r\n not being present. Perhaps this was on a foreign system, or some other encoding?

     

    If you do not believe me see http://codinghut.com/Files/proof.jpg, it says it all.

    In my defense, I did not look closely enough and thought the OP wrote Eder's post also. And I explain @ because in your previous post you use make comparisons that just are not true.

    Eder, Please describe in more detail exactly what you are trying to accomplish if your question has not already been answered.


    -Vaelek

    Wednesday, May 28, 2008 11:37 PM
  • First of all, I know its a very old thread :) However, I have the same question as Eder F. Dias and I don't think anyone has really understood his question.

    In Hyper Terminal a Carriage Return (\r) just returns the cursor to the start of the line (the same line!) while New Line (\n) just puts the cursor in a new line at the same position it was in the previous line. So to have the terminal act as we are used to in the Text Box you need to add \n\r at the end of a line.

    Based on this you should now understand how "1111111\r222222222"; can become "222222222"; (\r puts the cursor at the beginning of the row and then the twos start overwriting the ones).

    So the question is, can the Text Box display \r as the Hyper Terminal? I would surely like to know! :)

    Tuesday, October 02, 2012 1:45 PM