VB.Net RichTextBox Formatting

Discussion in 'Scripting & Programming' started by Fergal1982, Oct 7, 2007.

  1. Fergal1982

    Fergal1982 Petabyte Poster

    4,196
    172
    211
    I've been having a problem with Richtextbox formatting, that I was hoping one of the other Devs here might be able to help me out with:

    I'm using a richtextbox control as my output for messages, and need to format part of the text in bold/different colour. After playing about a bit, I can get the desired text changed, and have the text proceeding it remain the normal colour.

    However, when a new message is added to the list, the unformatted text on the previous line reformats. Its a little difficult to explain, so heres an example form class for you to see what I mean (form only has an RTB control on it)

    Code:
    Public Class Form1 
        Dim Timer1 As Timer 
        Dim _LastIndex As Integer 
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
            Timer1 = New Timer 
            Timer1.Interval = 100 
            AddHandler Timer1.Tick, AddressOf test 
            Timer1.Start() 
        End Sub 
    
        Private Sub test(ByVal sender As Object, ByVal e As System.EventArgs) 
            Const tstring As String = "Deek: Test" 
            RichTextBox1.Text &= tstring & vbCrLf 
    
            Dim colonIndex As Integer = RichTextBox1.Text.IndexOf(":"c, _LastIndex) 
            Dim cLength As Integer = colonIndex - _LastIndex + 1 
    
            RichTextBox1.Select(_LastIndex, cLength) 
            RichTextBox1.SelectionColor = Color.Red 
    
            RichTextBox1.SelectionStart = colonIndex + 2 
            RichTextBox1.SelectionColor = Color.Black 
    
            'Select length + 1 to stop highlighting of text on screen. 
             RichTextBox1.SelectionStart = RichTextBox1.Text.Length + 1 
    
            _LastIndex = RichTextBox1.Text.Length + 1 
        End Sub 
    End Class 
    Can anyone see what im doing wrong here?
     
    Certifications: ITIL Foundation; MCTS: Visual Studio Team Foundation Server 2010, Administration
    WIP: None at present
  2. ThomasMc

    ThomasMc Gigabyte Poster

    1,507
    49
    111
    is this what you mean?

    Code:
    Public Class Form1
        Dim Timer1 As Timer
        Dim _LastIndex As Integer
        
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Timer1 = New Timer
            Timer1.Interval = 100
            AddHandler Timer1.Tick, AddressOf test
            Timer1.Start()
        End Sub
    
        Private Sub test(ByVal sender As Object, ByVal e As System.EventArgs)
            Const tstring As String = "Deek: Test"
    
            RichTextBox1.AppendText(tstring & vbCrLf)
    
            Dim colonIndex As Integer = RichTextBox1.Text.IndexOf(":"c, _LastIndex)
            Dim cLength As Integer = colonIndex - _LastIndex + 1
    
    
            RichTextBox1.Select(_LastIndex, cLength)
            RichTextBox1.SelectionColor = Color.Red
    
            RichTextBox1.SelectionStart = colonIndex + 2
            RichTextBox1.SelectionColor = Color.Black
    
            'Select length + 1 to stop highlighting of text on screen. 
            RichTextBox1.SelectionStart = RichTextBox1.Text.Length + 1
    
    
            _LastIndex = RichTextBox1.Text.Length + 1
        End Sub
    End Class
    
     
    Certifications: MCDST|FtOCC
    WIP: MCSA(70-270|70-290|70-291)
  3. Fergal1982

    Fergal1982 Petabyte Poster

    4,196
    172
    211
    no, the adding of the text works fine, its not that - both the .appendtext and the &= method work (although .appendtext might be a bit neater to use, granted).

    The problem is in the colour formatting. Watch the previous line's colouring when a new line is added.

    Edit: Never mind, that does actually almost seem to be working - although the first letter on the selection isnt reformatting to the red/bold colour.
     
    Certifications: ITIL Foundation; MCTS: Visual Studio Team Foundation Server 2010, Administration
    WIP: None at present
  4. ThomasMc

    ThomasMc Gigabyte Poster

    1,507
    49
    111
    Try
    Code:
    Public Class Form1
        Dim Timer1 As Timer
        Dim _LastIndex As Integer
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Timer1 = New Timer
            Timer1.Interval = 100
            AddHandler Timer1.Tick, AddressOf test
            Timer1.Start()
        End Sub
    
        Private Sub test(ByVal sender As Object, ByVal e As System.EventArgs)
            Const tstring As String = "Deek: Test"
    
            RichTextBox1.AppendText(tstring & vbCrLf)
    
            Dim colonIndex As Integer = RichTextBox1.Text.IndexOf(":"c, _LastIndex)
            Dim cLength As Integer = colonIndex - _LastIndex + 1
    
    
            RichTextBox1.Select(_LastIndex, cLength)
            RichTextBox1.SelectionColor = Color.Red
            
            RichTextBox1.SelectionStart = colonIndex + 2
            RichTextBox1.SelectionColor = Color.Black
    
            'Select length + 1 to stop highlighting of text on screen. 
            RichTextBox1.SelectionStart = RichTextBox1.Text.Length
    
    
            _LastIndex = RichTextBox1.Text.Length
    
        End Sub
    End Class
    
     
    Certifications: MCDST|FtOCC
    WIP: MCSA(70-270|70-290|70-291)
  5. Fergal1982

    Fergal1982 Petabyte Poster

    4,196
    172
    211
    yep, its ok, got it working in the end.

    Cheers.
     
    Certifications: ITIL Foundation; MCTS: Visual Studio Team Foundation Server 2010, Administration
    WIP: None at present
  6. ThomasMc

    ThomasMc Gigabyte Poster

    1,507
    49
    111
    good stuff :D
     
    Certifications: MCDST|FtOCC
    WIP: MCSA(70-270|70-290|70-291)

Share This Page

Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.