Results 1 to 6 of 6

Thread: [RESOLVED] Help please - Richtextbox1 colorchange

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2012
    Posts
    171

    Resolved [RESOLVED] Help please - Richtextbox1 colorchange

    Hello, ive already made a thread for this but is really long and confusing so heres another.

    Im using this code:

    Code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            IE.RichTextBox1.SelectionColor = Color.Blue
            IE.RichTextBox1.AppendText(DateTime.Now & "                    " & LOGON.MaskedTextBox1.Text & "                    " & IE.Label6.Text & TextBox22.Text & TextBox22.Text)
            IE.RichTextBox1.SelectionColor = Color.Red
            IE.RichTextBox1.AppendText(TextBox21.Text & TextBox22.Text & TextBox22.Text & TextBox22.Text)
            Me.Close()
        End Sub
    and it has this result:

    Name:  work.png
Views: 191
Size:  7.0 KB


    The intention was to have the datetime, user id etc in blue and the remarks (Which is the text beaneath) in red, this works the first time round but doesnt there after that. Am i doing anything wrong in the coding?

    Cheers

  2. #2

    Thread Starter
    Addicted Member
    Join Date
    Oct 2012
    Posts
    171

    Re: Help please - Richtextbox1 colorchange

    I just found out it works in another richtextbox but not this one for some reason. anyone know why?

  3. #3
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Help please - Richtextbox1 colorchange

    Try this:

    1. Add this method to your class (or some global class/module)
    vb.net Code:
    1. Sub AppendTextToRTB(rtb As RichTextBox, color As Color, text As String)
    2.     rtb.SelectionStart = rtb.TextLength
    3.     rtb.SelectionLength = 0
    4.     rtb.SelectionColor = color
    5.     rtb.SelectedText = text
    6. End Sub

    2. This is the sample usage:
    vb.net Code:
    1. Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    2.     AppendTextToRTB(RichTextBox1, Color.Blue, "This is some text in blue" & vbCrLf)
    3.     AppendTextToRTB(RichTextBox1, Color.Red, "This is some text in red" & vbCrLf)
    4.     AppendTextToRTB(RichTextBox1, Color.Black, "This is some text in black" & vbCrLf)
    5. End Sub

    In your case, this should work
    (not tested)
    vb.net Code:
    1. Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    2.     AppendTextToRTB(IE.RichTextBox1, Color.Blue, DateTime.Now & "                    " & LOGON.MaskedTextBox1.Text & "                    " & IE.Label6.Text & TextBox22.Text & TextBox22.Text & vbCrLf)
    3.     AppendTextToRTB(IE.RichTextBox1, Color.Red, TextBox21.Text & TextBox22.Text & TextBox22.Text & TextBox22.Text & vbCrLf)
    4. End Sub
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Oct 2012
    Posts
    171

    Re: Help please - Richtextbox1 colorchange

    that works great thanks!!!!! 1 more quick one. How do i make the red text only become bold? cheers

  5. #5
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Help please - Richtextbox1 colorchange

    Just add an overloaded method that accepts the font you want to set:
    vb.net Code:
    1. Sub AppendTextToRTB(rtb As RichTextBox, color As Color, text As String)
    2.     rtb.SelectionStart = rtb.TextLength
    3.     rtb.SelectionLength = 0
    4.     rtb.SelectionColor = color
    5.     rtb.SelectedText = text
    6. End Sub
    7.  
    8. Sub AppendTextToRTB(rtb As RichTextBox, color As Color, font As Font, text As String)
    9.     rtb.SelectionStart = rtb.TextLength
    10.     rtb.SelectionLength = 0
    11.     rtb.SelectionColor = color
    12.     rtb.SelectionFont = font
    13.     rtb.SelectedText = text
    14. End Sub

    Usage:
    vb.net Code:
    1. Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    2.     Dim boldFont As New Font(RichTextBox1.Font, FontStyle.Bold)
    3.  
    4.     AppendTextToRTB(RichTextBox1, Color.Blue, "This is some text in blue" & vbCrLf)
    5.     AppendTextToRTB(RichTextBox1, Color.Red, boldFont, "This is some bold text in red" & vbCrLf)
    6.     AppendTextToRTB(RichTextBox1, Color.Black, "This is some text in black" & vbCrLf)
    7. End Sub
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Oct 2012
    Posts
    171

    Re: [RESOLVED] Help please - Richtextbox1 colorchange

    works like a dream, thanks!!

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