3

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:

Image of Chat

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

C-Pound Guru
  • 15,967
  • 6
  • 46
  • 67

1 Answers1

1

Keep the username and message apart, then set the colour and write them individually, E.g. 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
Alex K.
  • 171,639
  • 30
  • 264
  • 288
  • That doesn't work. The whole chatbox goes messy, sended text are not displayed at all and all i see in the chatbox is 4: where 4 is red and bold and the : are regular black color. – zeroClarity Feb 01 '17 at 17:08
  • It works fine for me, it will only display 4 if you pass it 4 – Alex K. Feb 01 '17 at 17:10
  • Note my code does not include or use the `0` in `AddChatMsg 0, ` in your example as it did not seem to be used, pass it 2 strings only. – Alex K. Feb 01 '17 at 17:11
  • Maybe we got a missunderstanding or something. Here's a short video of how the chatbox normally works: https://i.gyazo.com/2152d00700553c492561d9b6082efe50.mp4 I want only the username (in this case it's "test") to be red and bolded and the rest should stay as it is.. And this is what happens when I use your code as so: Can't put the code here, cause I run out of characters left, so here's picture: https://i.gyazo.com/552578a98bb777d7e51add260a71ec1f.png And this happens when using your code: https://i.gyazo.com/2b9981b6fab4d454e50a8ba21806452f.mp4 Thanks. – zeroClarity Feb 01 '17 at 17:31
  • I meant it to be used like this: https://1drv.ms/u/s!Ao96GSLZTCw-sy0wmIMxxXR00l_T – Alex K. Feb 01 '17 at 17:38
  • Well it looks like this method doesn't work with my application. Maybe the cause is that there's a code behind that textbox (I mean the way that it works) and that prevent your code from working in my case or something. Can you help me to get it to work with my code, please? Let me know what part of the code you need. – zeroClarity Feb 01 '17 at 17:48
  • You have `seltext=usernamelength` which writes the length to the box which is not correct. The principle is you don't need to worry about sellength, just set the selcolor/selbold and the *next* thing you write will be in that colour/bold. – Alex K. Feb 02 '17 at 11:08
  • You add text to the top, my code adds to the bottom, change .SelStart = Len(.Text) to .SelStart = 0 (or 1 if that fails) – Alex K. Feb 02 '17 at 11:08