RichtextBox trim problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • DavidAM
    New Member
    • Aug 2007
    • 3

    #1

    RichtextBox trim problem

    Hello,

    In my code I use the following command:

    me.richtextbox1 .text = me.richtextbox1 .text.trim

    By doing this I lose al my text formatting, it seems that by setting the text property of a richtextbox you lose al the formatting.
    Any idea how I can remove the leading and trailing spaces in a text without losing the rtf formatting?

    Thanks in advance.

    David
  • DavidAM
    New Member
    • Aug 2007
    • 3

    #2
    Already found a way to resolve this:

    Public Shared Sub TrimRichTextBox (ByRef RichTextControl As RichTextBox, _
    Optional ByVal LeadingSpaces As Boolean = True, _
    Optional ByVal TrailingSpaces As Boolean = True)
    Try
    If LeadingSpaces Then
    Do While RichTextControl .Text.StartsWit h(" ")
    RichTextControl .SelectionStart = 0
    RichTextControl .SelectionLengt h = 1
    RichTextControl .SelectedText = ""
    Loop
    End If
    If TrailingSpaces Then
    Do While RichTextControl .Text.EndsWith( " ")
    RichTextControl .SelectionStart = RichTextControl .Text.Length - 1
    RichTextControl .SelectionLengt h = 1
    RichTextControl .SelectedText = ""
    Loop
    End If
    Catch ex As Exception
    Throw New Exception(Syste m.Reflection.Me thodInfo.GetCur rentMethod.Name , ex)
    End Try
    End Sub

    Originally posted by DavidAM
    Hello,

    In my code I use the following command:

    me.richtextbox1 .text = me.richtextbox1 .text.trim

    By doing this I lose al my text formatting, it seems that by setting the text property of a richtextbox you lose al the formatting.
    Any idea how I can remove the leading and trailing spaces in a text without losing the rtf formatting?

    Thanks in advance.

    David

    Comment

    • kenobewan
      Recognized Expert Specialist
      • Dec 2006
      • 4871

      #3
      Well done, I assume that lefttrim & righttrim would have worked also...

      Comment

      Working...