VB Rich Text Box Coloring

Discussion in 'OT Technology' started by GOGZILLA, Jul 14, 2006.

  1. GOGZILLA

    GOGZILLA Double-Uranium Member

    Joined:
    Jan 16, 2003
    Messages:
    10,760
    Likes Received:
    3
    Location:
    Plantation, FL
    Code:
    Private Sub Command1_Click()
        Dim start As Integer
        Dim last As Integer
        Dim string1 As String
        Dim string2 As String
        Dim string3 As String
        Dim string4 As String
        
        
        string1 = "HELLO" + vbNewLine
        string2 = "THERE" + vbNewLine
        string3 = "MORE" + vbNewLine
        string4 = "BRISKET" + vbNewLine
        
        start = 0
        rtb1.Text = rtb1.Text + string1
        last = Len(rtb1.Text)
        rtb1.SelStart = start
        rtb1.SelLength = last - start
        rtb1.SelColor = vbRed
        
        start = last
        
        rtb1.Text = rtb1.Text + string2
        last = Len(rtb1.Text)
        rtb1.SelStart = start
        rtb1.SelLength = last - start
        rtb1.SelColor = vbBlue
        
        start = last
        
        rtb1.Text = rtb1.Text + string3
        last = Len(rtb1.Text)
        rtb1.SelStart = start
        rtb1.SelLength = last - start
        rtb1.SelColor = vbBlack
        
        start = last
        
        rtb1.Text = rtb1.Text + string3
        last = Len(rtb1.Text)
        rtb1.SelStart = start
        rtb1.SelLength = last - start
        rtb1.SelColor = vbGreen
        
        start = last
        
    End Sub
    
    
    It is a rudimentary program but it shows my problem very well. I've read all about rich text box coloring and to my best knowledge on how to change the text color you set SelStart to where you want to begin, then you set SelLength to the number of characters you want to color and then set SelColor to the color you want. The program works fine until it colors the third string at which point all the text above the third string is turned red. I have stepped through it in the debugger and all my variable values are correct I am not sure if I am fucking up the rich text formatting somewhere in there somehow. VB6 rich text box's have .Text and .TextRTF and I'm not sure which to use with formatting circumstances. I know .TextRTF gives the text with the rich text format too but am still lost on my problem. Any input is appreciated.
     
  2. deusexaethera

    deusexaethera OT Supporter

    Joined:
    Jan 27, 2005
    Messages:
    19,712
    Likes Received:
    0
    Try concatenating all of the text strings BEFORE applying the color formatting to the bits and pieces. Then use "rtb1.SelLength = len(string*)" to set the SelLength value.

    Also (and this is something you'll have to experiment with), I think that you don't have to block off a piece of text to change its color; instead, I believe that you simply insert a color-change code into the text string, and then all text after that code shows up in the changed color until another color-change code is encountered. If Rich Text didn't work like this, then MSWord (for example) would need to reapply font- and color-change commands to the last block of text each time you typed a new character at the end of the document.
     
  3. GOGZILLA

    GOGZILLA Double-Uranium Member

    Joined:
    Jan 16, 2003
    Messages:
    10,760
    Likes Received:
    3
    Location:
    Plantation, FL
    Fixed the problem. Doing statements like

    rtb.Text = rtb.Text + blahblah

    messes up the RTF formatting. If you use

    rtb.SelText = rtb.SelText + blahblah

    it keeps it all. I hate getting hung up on stupid shit like this not to mention the complete lack of documentation on MS for vb6, its all .net now which id prefer but its not a company standard :hsd:
     
  4. deusexaethera

    deusexaethera OT Supporter

    Joined:
    Jan 27, 2005
    Messages:
    19,712
    Likes Received:
    0
    VB.NET is a piece of shit. It attempts to make VB a legitimate language for anything other than scripting and GUI design, and thus it eliminates the main reason for actually using VB. If you must use .NET, learn C#; it's the best language in the .NET suite.
     

Share This Page