简体   繁体   中英

Change the color of a certain part of the text 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?

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!

I just need to show some text in a Label and then change the color of one or two letters only .

I've created an XLabel(0) Label control, and set .Visible = False and .Autosize = True . Then I'm basically reading one string at a time from an array and loading new XLabel() controls, one for each letter in the string.

For z = 1 To Len(a)
    Load XLabel(z)
    With XLabel(z)
        .Caption = Mid(a, z, 1)
        .Left = XLabel(z - 1).Left + XLabel(z - 1).Width
        .BackColor = vbWhite
        .Visible = True
    End With
Next z

Since Autosize is on, all labels sit close to one another the same way they would inside a single Label Control.

But this way you can modify a single letter the way you need to.

Remember to unload all controls before moving on, if ever.

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