Results 1 to 9 of 9

Thread: Am I stupid or this hard?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2000
    Posts
    367

    Does anyone here know how to keep text in a richTextbox control to have its original coloring? to see what I mean check out the below code and explanation.

    Here is an example of what I mean


    Code:
    Private Sub Command1_Click()
    
    Dim x As Integer
    
    
    If (color) Then
         rtb.SelStart = 0
         rtb.SelLength = Len(rtb.Text)
         rtb.SelColor = vbRed
    End If
    MsgBox rtb.SelStart
    If (color) Then
         x = Len(rtb.Text)
         rtb.Text = rtb.Text & " Yahoo "
         rtb.SelStart = x
         rtb.SelLength = 7
         rtb.SelColor = vbBlue
    Else
         x = Len(rtb.Text)
         rtb.Text = rtb.Text & " Yahoo "
         rtb.SelStart = x
         rtb.SelLength = 7
         rtb.SelColor = vbGreen
    End If
    
    color = False
    End Sub
    
    Private Sub Form_Load()
    
    color = True
    
    End Sub
    So when I click the command once, it colors the beginning text of the rtb to red, then it adds the word yahoo and colors it blue. But when I click it again, it adds the old text and covers the it all red, then it adds the second yahoo and colors it green. But I lost the original color of blue on the first yahoo.


  2. #2
    Fanatic Member
    Join Date
    Feb 2000
    Location
    Japan
    Posts
    840
    Of course it did, That's what this code does!

    Here's the flow
    - click button
    - The first 'if' sets all text red
    - The second if else follows the if part, adds "yahoo" to the end and coloUrs (I'm an Aussie ) it (yahoo) blue

    -click again
    -Len(rtb.Text) now includes the previous yahoo so it, along with all the other text get's coloured red
    -Then we follow the else path, we add yahoo again onto the text and colour it (the last yahoo) green

    if you want to leave the first yahoo blue then you need a public variable with the old length, then only change to red up to there, then change to green from the end of the new length.

    Please, next time a few more code comments, I'm not that good at reading other's code without them.

    Cheers

    Paul Dwyer
    Network Engineer
    Aussie In Tokyo

    Using Powerbasic 6 & VB6 SP4 (Please also add your VB Version to your signature!)

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    May 2000
    Posts
    367
    That wont work though, I am going to be adding text as it comes in, and I need to be able to have all the old retain its color. Did I explain that good enough?

    Let me try a situation that maybe my situation could apply to.

    Say, I was making a chat server, where 5 people were on, and they all had different coloured (is that right austrailian?) So when a client recieves information it first would receive a colour, then the text, so I cant really keep track of the location or size for every time someone types something, for that is poor efficency. But how will the rich text box keep the colouring for the correct text? (Hmm, I think I just confused myself on that explanation )

    Do you see what I am trying to say/do? If not I will try again to explain it.

  4. #4
    Addicted Member Jakys's Avatar
    Join Date
    Dec 1999
    Location
    Norway
    Posts
    180

    Question If..

    If you are working on a chatapp, could you please mail it to me ([email protected]). I was doing the same thing for a couple of months ago, but i didn't make it work!

  5. #5
    Fanatic Member
    Join Date
    Mar 2000
    Location
    That posh bit of England known as Buckinghamshire
    Posts
    658

    Thumbs up Give this a whirl

    I know this is not the best solution in the world, but it works. I have tried a couple of other methods but to no avail.

    The reason i could not get it to work is that when you re-set the text with RichText1.Text = RichText1.Text & myStr it is not like typing in a few new characters, it is infact like re-typing the whole thing.

    Thsi gave me the clue that i needed. use SendKeys. After crashing my computer several times, i mean really crashing i have it working.

    Code:
    Option Explicit
    
    
    Private Sub Command1_Click()
        Dim x As Integer, y As Integer, z As Integer
        Dim myStr As String
        
        Randomize
        x = Int(255 * Rnd)
        y = Int(255 * Rnd)
        z = Int(255 * Rnd)
        
        'set the text to a random colour.
        RichTextBox1.SelColor = RGB(x, y, z)
        
        'just a demo string
        myStr = Chr(13) & "User " & x & " :  Hello"
        
        'set the focus
        RichTextBox1.SetFocus
        'set the cursor to the end of the text box.
        RichTextBox1.SelStart = Len(RichTextBox1.Text)
        'send the string to the text box.
        SendKeys myStr
        
    End Sub
    Hope this hepls

    Regards
    Iain, thats with an i by the way!

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    May 2000
    Posts
    367
    Iain you little programming god you.

    it works beautifully!!!!!!!!!!!

    Thank you

    [Edited by billrogers on 05-25-2000 at 04:26 PM]

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    May 2000
    Posts
    367
    Does anyone know why this doesnt work? It is just a slight variation from Ian's example.

    Code:
    Private Sub addServerText(ByVal x As Integer, ByVal y As Integer, _
                              ByVal z As Integer, ByVal myStr As String)
    
        
        ' set the text to the selected color
        rtbReceive.SelColor = RGB(x, y, z)
        myStr = myStr & Chr(13)
        ' set the focus
        rtbReceive.SetFocus
        ' set the cursor to the front of the text box
        rtbReceive.SelStart = 0
        ' unlock the text bock
        rtbReceive.Locked = False
        'DoEvents
        SendKeys myStr
        DoEvents
        rtbReceive.Locked = True
        txtSend.SetFocus
    
    End Sub
    This doesnt switch colors, for some reason it stays with the first color it gets and continues to use that color even though, i set the color differntly when this is called.

    [Edited by billrogers on 05-26-2000 at 10:00 AM]

    [Edited by billrogers on 05-26-2000 at 10:00 AM]

  8. #8
    Fanatic Member
    Join Date
    Mar 2000
    Location
    That posh bit of England known as Buckinghamshire
    Posts
    658
    Yep i do indeed know why.

    When you set the colour, you set it for where the cursor is. So first move the cursor, then set the colour.

    Code:
    Private Sub addServerText(ByVal x As Integer, ByVal y As Integer, _
                              ByVal z As Integer, ByVal myStr As String)
    
        
        myStr = myStr & Chr(13)
        ' set the focus
        rtbReceive.SetFocus
        ' set the cursor to the front of the text box
        rtbReceive.SelStart = 0
        
        ' set the text to the selected color
        rtbReceive.SelColor = RGB(x, y, z)
        
        ' unlock the text bock
        rtbReceive.Locked = False
        'DoEvents
        SendKeys myStr
        DoEvents
        rtbReceive.Locked = True
        txtSend.SetFocus
    
    End Sub
    Easy hey
    Iain, thats with an i by the way!

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    May 2000
    Posts
    367
    [Bill bows to Iain's greatness]

    Thanks Iain. you da man

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