Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change the color of a certain part of the text VB6?

Tags:

text

colors

vb6

This is what my code looks like

Form1.GRQ.AddItem txtRequest.Text & (" - Pending")

I just want to change the ( - Pending) part to red so it appears red next to the black text in the listbox. any ideas?

like image 248
happycamper1221 Avatar asked May 27 '11 10:05

happycamper1221


1 Answers

Using regular VB6 controls, unfortunately, you can't do this. You can change the color of all the text of a textbox/listbox/label using .ForeColor, but not parts of it, and that's really no good for you. Thankfully, there are two solutions:

  1. The first is to continue to use the listbox as you have it, but add in a caption with red text reading " - Pending" next to the text you want. It's not pretty, but you can make it work.

  2. The better solution is to get more familiar with the RichTextBox control. This will only work if you have the Professional or Enterprise versions of VB6, though. Assuming you do, on the VB6 menu, click Project -> Components, and then in the new window that pops up, under the Controls tab, check "Microsoft Rich TextBox Control 6.0" and then click OK. The RichTextBox option should appear on the Toolbox, you can add it to the form like any other object, and it'll act like a combination listbox/textbox... it's very useful. If you want some documentation on it, check out the MSDN.

    Unfortunately, RichTextBox kinda stinks in terms of changing the color of text. It can be done, but not with a simple command. You have to find the text you want, select it, and then set the color. (This also goes if you want to change the color of all the text - you have to select it all first.) Anyway, the way to do that would be:

    RichTextBox1.SelStart = RichTextBox1.Find(" - Pending")
    RichTextBox1.SelLength = 10
    RichTextBox1.SelColor = vbRed

I hope all this helps. Best of luck!

like image 199
erekalper Avatar answered Nov 07 '22 09:11

erekalper