在VB6 RichTextBox

时间:2017-02-01 01:01:44

标签: vb6 richtextbox textcolor text-formatting

所以我有这个实际上是游戏聊天的文本框。当有人键入消息时,它会如下所示:UserName:Message

我想要做的是以某种方式使UserName文本始终以不同的颜色显示,因此它与实际消息分开。

以下是我目前使用的代码:

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

这实际上运作良好,但用户名仅在第一个文本行中为红色:

Image of Chat

如何让它适用于每一行?如果可能的话,将字体更改为粗体也会很棒。谢谢!

1 个答案:

答案 0 :(得分:1)

保持用户名和信息分开,然后设置颜色并单独写下,例如。打电话给:

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

使用:

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