Results 1 to 18 of 18

Thread: [RESOLVED] Formatting color for creatin text part in VB6 RichTextBox

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2016
    Posts
    47

    Resolved [RESOLVED] Formatting color for creatin text part in VB6 RichTextBox

    Resolved
    Last edited by Edelweise; Feb 4th, 2017 at 05:19 AM.

  2. #2
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Formatting color for creatin text part in VB6 RichTextBox

    Using the method you have there you would use .selstart to tell it where to begin the color looks like it has been omitted and is using 0

    You could also use the RTF formatting codes when adding text to the control but you would need to search for those.

  3. #3

    Thread Starter
    Member
    Join Date
    Mar 2016
    Posts
    47

    Re: Formatting color for creatin text part in VB6 RichTextBox

    The start should begin at 0 and end in the last username symbol, so it will select the username only. I don't think theres something wrong with this one.
    Can you explain a little bit more detailed about RTF formatting, please ?

  4. #4
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Formatting color for creatin text part in VB6 RichTextBox

    Your code selects the number of characters that are in the user name starting from the beginning of the text. So if your user name was John for example it is selecting the first 4 characters in the text box then setting the color of those characters.

    If the user name appears again at say position 50 your code will select only the first 4 characters starting at position 0 and color the same text that it colored before making it appear to do nothing at all and so on no matter how many times the name appears.


    Not enough code the to be sure but what you need to know is where the user name is in the text box and then use that position as your starting point each time.

    So before you add the text to the box you need to know the position which you will need to use later to set the color
    Code:
    Dim StartPosition as Long
    StartPosition=Len(MyForm.ChatText.Text)
    AddChatMsg 0, userName & ": " & theMsg 'send it to the chatbox
    
    'then later in your code
    
    .SelStart=StartPosition
    .SelLength = userNameLength 'set Length
    .SelColor = vbRed 'change color
    Note: You may need to alter the value of the startposition depending on how you are adding data to the text box in that sub routine

    Edit: Actually It may not always be starting at 0 omitting that start position value may make it use the current cursor position as a starting point which may be at the end of the last text entered or somewhere else depending on how you have coded your sub that adds the text
    Last edited by DataMiser; Feb 1st, 2017 at 01:21 PM.

  5. #5
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Formatting color for creatin text part in VB6 RichTextBox

    Another note: The previous post assumes that you are adding the text to the bottom of the text in the text box. If you are adding it to the top then what you had already should work

    In other words if your code does something like this

    Code:
    RTB1.Text=RTB1.Text & ChatText & vbcrlf
    then you need to do something like I showed as only the very first entry will be in the 0 position of the text box and each additional entry will be further down.

    If your code does something like this
    Code:
    RTB1.Text=ChatText & vbcrlf & RTB1.Text
    Then the most recent entry will always be at the top of the text box and your existing code should work.

    So again it depends on the way you are adding the text

  6. #6

    Thread Starter
    Member
    Join Date
    Mar 2016
    Posts
    47

    Re: Formatting color for creatin text part in VB6 RichTextBox

    Resolved
    Last edited by Edelweise; Feb 4th, 2017 at 05:19 AM.

  7. #7
    Frenzied Member
    Join Date
    Jun 2014
    Posts
    1,084

    Re: Formatting color for creatin text part in VB6 RichTextBox

    every windows comes with wordpad
    every windows comes with notepad
    write in wordpad what you want to see,save it
    open the wordpad document with notepad
    you will clearly see what rtf codes you need for colors,fonts,etc...
    do not put off till tomorrow what you can put off forever

  8. #8
    Frenzied Member
    Join Date
    Jun 2014
    Posts
    1,084

    Re: Formatting color for creatin text part in VB6 RichTextBox

    come to think of it
    maybe it is easier using a flexgrid
    just drop a flexgrid on a form, and name it "fg" (without the quotes)

    Code:
    Private Sub Command1_Click()
       Static i As Integer
       i = i + 1
       fg.AddItem "aaa" & Str(i) & vbTab & "bbb" & Str(i),1
       fg.Col = 0
       fg.Row = 1
       fg.CellForeColor = vbRed
       fg.Col = 1
       fg.CellFontBold = True
    End Sub
    do not put off till tomorrow what you can put off forever

  9. #9
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Formatting color for creatin text part in VB6 RichTextBox

    Quote Originally Posted by Edelweise View Post
    In my chatbox, the latest message is always at top and my current code:

    Code:
    Dim curColor As Long
    Dim userNameLength As Integer
    userNameLength = Len(PlayerName)
    With Main.PrivateChatText
       curColor = .SelColor 'set curColor
       .SelLength = userNameLength 'set Length
       .SelColor = vbRed 'change color
       .SelText = PlayerName 'not sure wha this do
       .SelColor = curColor 'update current color var
    End With
    Works, but it will only colorize the username in red in the top line, leaving the rest chat lines unformatted as so:
    https://i.gyazo.com/03d36eb44e16a9be...57d8e93a9e.mp4
    Well then I would suspect that it is the way you are adding the text.

    You should show the code that actually adds the text to the box. What you have shown will color only the first portion of the text box. Without seeing the code that writes to the text box there is no way to know what might be happening there.

  10. #10
    Hyperactive Member
    Join Date
    Oct 2016
    Posts
    369

    Re: Formatting color for creatin text part in VB6 RichTextBox

    RichTextBox is not the best tool to use for a chat box. Sooner or later you will be asking how to do transparent and animated images and it doesn't support it.

  11. #11

    Thread Starter
    Member
    Join Date
    Mar 2016
    Posts
    47

    Re: Formatting color for creatin text part in VB6 RichTextBox

    Resolved
    Last edited by Edelweise; Feb 4th, 2017 at 05:20 AM.

  12. #12
    Hyperactive Member
    Join Date
    Oct 2016
    Posts
    369

    Re: Formatting color for creatin text part in VB6 RichTextBox

    Is below a typo

    Code:
    ' admin commands
        If Mid(message, 1, 4) = "@" And check = Len(checkmessage) Then
            SendMessage "4," & "@" & message
            Exit Sub
        End If

  13. #13

    Thread Starter
    Member
    Join Date
    Mar 2016
    Posts
    47

    Re: Formatting color for creatin text part in VB6 RichTextBox

    Resolved
    Last edited by Edelweise; Feb 4th, 2017 at 05:20 AM.

  14. #14
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Formatting color for creatin text part in VB6 RichTextBox

    The code is a bit hard to read, would help if it was properly indented.

    I do not see any place where you read the formatted text from the rtb. I do see a place where you are replacing it and that may be your issue.

  15. #15

    Thread Starter
    Member
    Join Date
    Mar 2016
    Posts
    47

    Re: Formatting color for creatin text part in VB6 RichTextBox

    Quote Originally Posted by DataMiser View Post
    The code is a bit hard to read, would help if it was properly indented.

    I do not see any place where you read the formatted text from the rtb. I do see a place where you are replacing it and that may be your issue.
    I am actually not the original creator of this game and I don't fully understand the code too. So where you see the place where the text is replaced ?

  16. #16
    Hyperactive Member
    Join Date
    Oct 2016
    Posts
    369

    Re: Formatting color for creatin text part in VB6 RichTextBox

    Quote Originally Posted by Edelweise View Post
    No. This is to detect admin commands, cause every admin command starts with "@" (e.g @kick user)
    Well, if it ain't a typo then you will have a run-time boo-boo since it will never enter the If clause. You can't compare a length of 4 (Mid(message, 1, 4) to a length of 1 ("@") and expect it to work
    Last edited by I Love VB6; Feb 2nd, 2017 at 04:12 PM.

  17. #17
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Formatting color for creatin text part in VB6 RichTextBox

    Quote Originally Posted by Edelweise View Post
    I am actually not the original creator of this game and I don't fully understand the code too. So where you see the place where the text is replaced ?
    The last two lines above end sub in the bottom piece of code you posted in post #11

    Code:
    Main.PrivateChatText.TextRTF = temp
    Bar.PrivateChatText.TextRTF = temp
    That will replace whatever is in the rtb with whatever is in temp

  18. #18

    Thread Starter
    Member
    Join Date
    Mar 2016
    Posts
    47

    Re: Formatting color for creatin text part in VB6 RichTextBox

    Thank you for showing me this @DataMiser !
    When I checked what temp is, I saw that it contains the text that is added to the chatbox with all it's RTF options (e.g \cf1user:message) where cf1 is previously declared color (e.g \colortbl ;\red0\green255\blue255, so I declared my own color, make a string variable, stored the username in the string variable and just did:
    Code:
    Replace(temp, "\cf1" & userName & " : ", "\cf2" & userName & " : \cf1")
    So it will colorize the username and proceed with other text with different color and it worked like a charm. Thanks for all the answers

Tags for this Thread

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