Results 1 to 5 of 5

Thread: [RESOLVED] Replace characters in RichTextBox?

  1. #1

    Thread Starter
    Addicted Member tgf-47's Avatar
    Join Date
    Feb 2010
    Location
    CapeTown, South Africa -34.01244,18.337415
    Posts
    209

    Resolved [RESOLVED] Replace characters in RichTextBox?

    I'm using something like this:
    vb.net Code:
    1. Dim tmp As String = RichTextBox.Text
    2.         tmp = tmp.Replace("[", "{")
    3.         tmp = tmp.Replace(Space(1), "_")

    How do I find all the "Enters" and replace them with a "^"?

    I have tried vbcrlf and chr(13), but nothing

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Replace characters in RichTextBox?

    It's important to understand what a line break is. In Windows, the standard line break is a combination of a carriage return character (ASCII 13) and a line feed character (ASCII 10). This combination can be represent in various ways. Those who've worked with C-based languages will recognise "\r\n", which is not supported in VB. In VB you can use vbCrLf, ControlChars.CrLf, ControlChars.NewLine or Environment.NewLine. On non-Windows systems, the standard line break is a line feed character alone, which can be represented using vbLf or ControlChars.Lf.

    If I remember correctly, the RichTextBox uses line feeds only for line breaks. Maybe that's a standard for RTF, I don't know, but it would explain why your searching for CR/LF or just CR didn't work: you need to search for just LF.

    For completeness, what you might like to do is first trying replacing all CR/LF pairs, then replace lone LFs. That way you will be sure to get every line break regardless of whether it's the Windows or non-Windows type.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Replace characters in RichTextBox?

    Also, why do this:
    vb.net Code:
    1. tmp = tmp.Replace(Space(1), "_")
    when you could just do this:
    vb.net Code:
    1. tmp = tmp.Replace(" ", "_")
    Also, I think that you'll find that, if your replacements involve only a single character in and out, it's probably more efficient to actually use Chars instead of Strings:
    vb.net Code:
    1. tmp = tmp.Replace(" "c, "_"c)
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Addicted Member tgf-47's Avatar
    Join Date
    Feb 2010
    Location
    CapeTown, South Africa -34.01244,18.337415
    Posts
    209

    Re: Replace characters in RichTextBox?

    Thanks for the detailed explanation. I appreciate it.

  5. #5
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Replace characters in RichTextBox?

    Just a little addendum...
    On non-Windows systems, the standard line break is a line feed character alone, which can be represented using vbLf or ControlChars.Lf.
    And Environment.NewLine ... that's the point of Environment.NewLine ... it is driven by the environment. Which is why it's the recommended way to do linefeeds.

    http://msdn.microsoft.com/en-us/libr...t.newline.aspx
    Quote Originally Posted by MSDN
    A string containing "\r\n" for non-Unix platforms, or a string containing "\n" for Unix platforms.
    Also, I don't think RTF actually stores the LF character... seems I remember seeing it storing the RTF code for it {\lf} I think... or something like that... I also know there's a "para" identifier to denote paragraphs... not sure how that fits into all of this... never mind, I see you're dealing with the .Text property... in which case jmc is right... replace the LFs.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

Tags for this Thread

Posting Permissions

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



Click Here to Expand Forum to Full Width