简体   繁体   中英

Formatting color for creatin text part in VB6 RichTextBox

So I have this textbox that's actually a game chat. When someone type a message it appers like so: UserName: Message

What I want to do here is somehow make the UserName text always appear with different color, so it's kinda seperated from the actual message.

Here's the code I am currently using:

AddChatMsg 0, userName & ": " & theMsg 'send it to the chatbox

Dim curColor As Long 'current text color
Dim userNameLength As Integer 'username length

userNameLength = Len(UserName) 'store username in integer var
With MyForm.ChatText
   curColor = .SelColor 'set curColor
   .SelLength = userNameLength 'set Length
   .SelColor = vbRed 'change color
   .SelText = UserName 'not sure wha this do
   .SelColor = curColor 'update current color var
End With

This actually works well, but the username is red only in the first text line:

聊天的形​​象

How do I make it work for every line? Also if possible to change the font to bold will be awesome. Thank you!

Keep the username and message apart, then set the colour and write them individually, Eg call as:

AddChatMsg "DonaldTrump", "I like to grab em"

Using:

Sub AddChatMsg(UserName As String, theMsg As String)
    Dim curColor As Long 'current text color
    With MyForm.ChatText
        curColor = .SelColor
        .SelStart = Len(.Text) 'ensure we are at the end
        .SelColor = vbRed
        .SelBold = True  'write in bold
        .SelText = UserName
        .SelBold = False
        .SelColor = curColor
        .SelText = ": " & theMsg & vbCrLf
    End With
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM