Change the color of a specific piece of VB6 text?

This is what my code looks like

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

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

+6
source share
2 answers

With the usual VB6 controls, unfortunately, you cannot do this. You can change the color of the whole text of the text box / list / label using .ForeColor , but not parts of it, and this really is not suitable for you. Fortunately, there are two solutions:

  • First, to continue using the list as you have, but add a title with the red text β€œPending” next to the desired text. This is not very, but you can make it work.

  • The best solution is to become familiar with the RichTextBox control. This will only work if you have versions of VB6 Professional or Enterprise. Assuming what you are doing, select "Project β†’ Components" from the VB6 menu, and then in the new window that appears on the "Management" tab, select the "Microsoft Rich TextBox Control 6.0" checkbox and click "OK." The RichTextBox option should appear on the toolbar, you can add it to the form, like any other object, and it will act as a combinational list / text field ... this is very useful. If you need some kind of documentation, check out the MSDN .

    Unfortunately, the RichTextBox stinks in terms of changing the color of the text. This can be done, but not with a simple command. You must find the text you want, select it, and then set the color. (This also happens if you want to change the color of the entire text - you must first select all of it.) In any case, the way to do this is:

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

Hope all this helps. Good luck

+10
source

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

I created an XLabel(0) Label control and set .Visible = False and .Autosize = True . Then I basically read one line at a time from the array and load the new XLabel() controls, one for each letter in the line.

 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 enabled, all tags are seated next to each other in the same way as inside a single shortcut control.

But in this way you can change one letter as you need.

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

0
source

Source: https://habr.com/ru/post/889196/


All Articles