Inserting new lines into a 'RichTextBox' from a string?

32,940

I ended up finding my own solution to this problem; it's similar to what has been said but instead what this does is check for a specific character in the string and then remove it and in its place puts in a new line.

myLongString = myLongString.Replace("@", "" + System.Environment.NewLine);

The example above simply checks the string for the @ symbol and then removes it and adds a new line. This then allows the string to be formatted with a new line wherever the specified keyword or symbol appears which means that the formatting of the string can be stored with the XML node and then interpreted.

Share:
32,940
Kolors
Author by

Kolors

Q: How do you generate a random string? A: Put a Windows user in front of vi, and tell them to exit

Updated on March 14, 2020

Comments

  • Kolors
    Kolors about 4 years

    Currently in my application I have a string that is read in from an XML file however the whole string is concatenated together and I want to be able to split up the string over multiple lines of the 'RichTextBox' at specific points that I choose in the XML node for that string.

    For example my string before formatting as it sits in the XML node:

    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus eleifend arcu vel tellus aliquam eget aliquet orci dignissim. Integer volutpat congue elementum. In commodo porta sem. Phasellus commodo consectetur hendrerit. Integer bibendum consequat elit nec ultricies. Fusce facilisis elit in justo facilisis sagittis. Aenean eget risus placerat dui hendrerit pharetra sed in neque. Maecenas vehicula iaculis lectus eget scelerisque. Fusce sed consequat elit.

    The result I desire after formatting in my application:

    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus eleifend arcu vel tellus aliquam eget aliquet orci dignissim. Integer volutpat congue elementum. In commodo porta sem.

    Phasellus commodo consectetur hendrerit. Integer bibendum consequat elit nec ultricies. Fusce facilisis elit in justo facilisis sagittis.

    Aenean eget risus placerat dui hendrerit pharetra sed in neque. Maecenas vehicula iaculis lectus eget scelerisque. Fusce sed consequat elit.

    Is it possible to split up one long concatenated string over multiple lines like this for a 'RichTextBox' by including the formatting directly within the XML node / string itself rather than hard coding where each new line needs to be?

  • Kolors
    Kolors over 11 years
    This doesn't help because if my strings are different and have different lengths then my string will be split up incorrectly. I wanted to know if there is a way to split up a string from within the string itself.
  • Mahdi Tahsildari
    Mahdi Tahsildari over 11 years
    Explain more about what you want
  • Kolors
    Kolors over 11 years
    Basically I have one long string. However that string changes depending on the text in the XML file. Which means if I use your method it wont work on the different string as it will be a different length and different text which means splitting it up like that will stop it from making sense. This is why I want to be able to add something within either the XML file or the string itself rather than manually hard-coding each new line for each string.
  • Mahdi Tahsildari
    Mahdi Tahsildari over 11 years
    if you want to add them from the string itself then you can use \n like my name is \n mahdi tahsildari \n what is yours?
  • Kolors
    Kolors over 11 years
    All that does is print '\n' rather than actually creating a new line.
  • Kolors
    Kolors over 11 years
    Absolutely nothing. I've put it within the nodes of the XML file text and all it does is print \n. Could it be a property of the text box that needs changing?
  • Mahdi Tahsildari
    Mahdi Tahsildari over 11 years
    @KarlDaniel are you using @ before the string? MessageBox.Show(@"my name is \n mahdi tahsildari \n what is yours?"); and MessageBox.Show("my name is \n mahdi tahsildari \n what is yours?"); are different.
  • Mahdi Tahsildari
    Mahdi Tahsildari over 11 years
    in textboxes you should use Environment.NewLine
  • Kolors
    Kolors over 11 years
    I'm not using a message box. I just have a string that is completely un-formatted and I want to be able to embed new lines in it by having them within the string. I tried \n like you said but that just prints '\n' rather than actually making a new line.
  • Kolors
    Kolors over 11 years
    I've noticed rich text boxes populate themselves line by line through an array. I assume it would be possible therefore to split a string up over an array and then simply use that array to populate my rich text box.