CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Apr 2001
    Posts
    4

    RichTextBox Color Preservation

    I currently have the following code

    private Sub Command1_Click()
    RichTextBox1.Text = RichTextBox1.Text & vbNewLine & Text1.Text
    RichTextBox1.SelStart = InStr(len(RichTextBox1.Text) - len(Text1.Text), RichTextBox1.Text, Text1.Text) - 1
    RichTextBox1.SelLength = len(Text1.Text)
    If Option1.Value = true then
    RichTextBox1.SelColor = vbRed
    else
    RichTextBox1.SelColor = vbBlue
    End If
    RichTextBox1.SelLength = 0
    End Sub



    (where option1 says red and option2 says blue)
    (the way it works is you type text in Text1, and it adds that to RichText1, in the selected color)

    Eveytime I click command1, the whole textbox resets itself to black, and only the last thing i typed in has any color. What do I need to do?


  2. #2
    Join Date
    Mar 1999
    Location
    Nepal
    Posts
    540

    Re: RichTextBox Color Preservation

    Here's one way to get about it


    private Sub Command1_Click()
    Dim OldLen as Integer
    OldLen = len(RichTextBox1.Text)
    RichTextBox1.SelStart = OldLen
    RichTextBox1.SelText = vbCrLf
    OldLen = len(RichTextBox1.Text)
    RichTextBox1.SelText = Text1.Text
    RichTextBox1.SelStart = OldLen
    RichTextBox1.SelLength = len(Text1.Text)
    If Option1.Value = true then RichTextBox1.SelColor = vbRed else RichTextBox1.SelColor = vbBlue
    RichTextBox1.SelLength = 0
    End Sub





  3. #3
    Join Date
    Apr 2001
    Posts
    4

    Re: RichTextBox Color Preservation

    Hey, would it be too much to ask if you could explain what this is doing? Ive been looking at it and I'm totally lost.
    Thanks.


  4. #4
    Join Date
    Mar 1999
    Location
    Nepal
    Posts
    540

    Re: RichTextBox Color Preservation


    private Sub Command1_Click()
    Dim OldLen as Integer
    'Save the length of the text
    OldLen = len(RichTextBox1.Text)
    'set the insertion point at the end of the the rich text box
    RichTextBox1.SelStart = OldLen
    'Insert a newline character at the end, you could use vbNewLine
    RichTextBox1.SelText = vbCrLf
    'Compute the newlength. It is basically oldlen + 2
    OldLen = len(RichTextBox1.Text)
    'This adds the new text at the end of what is already in the rtb.
    RichTextBox1.SelText = Text1.Text
    'Selection starts at the old length (just before the new text)
    RichTextBox1.SelStart = OldLen
    'You know this...
    RichTextBox1.SelLength = len(Text1.Text)
    'Just wrote your three five lines of If...then in one line
    If Option1.Value = true then RichTextBox1.SelColor = vbRed else RichTextBox1.SelColor = vbBlue
    'Finally remove the selection
    RichTextBox1.SelLength = 0
    End Sub





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

Featured