Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Colored text in TextBox

Status
Not open for further replies.

dqrose

Technical User
Mar 17, 2006
24
GB
Is it possible to change the colors of words that are displayed in a textbox? For example, if I wanted to write "An error has occurred" in a textbox, and to write the word "error" in red, could it be done? I'm guessing that if it's possible at all, then I'd need to insert some control codes into the text string - but I've no idea what those codes might be.

Many thanks for any help received.

 
There are several ways of accomplishing this, but none of them (that come to mind) involve a text box control. The standard text box control allows you to change the fore color, but it applies to all the words in the text box (not just selected words).

Instead, you could use a Rich Text Box control to achieve your desired results.

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Hi George - that's perfect: I didn't previously know that there WAS such a control. I've now installed it, but I can't work out how to paste a rich text file into it in such a way that the color information is copied into it too.

For example, I've tried two ways to import an RTF file from the clipboard into the RTF textbox control (which I've called EditWindow), using

EditWindow.Text = Clipboard.GetText(&HBF01)

or

EditWindow.Text = Clipboard.GetText(1)

but the first one just drops in reams of RTF codes as black text, whereas the second just drops in the clipboard as plain black text. Is there something else I have to do to the control to make it work in the way I want?
 
Truth is, I don't have a lot of experience using the rich text box control. However, I know that it does support what you want.

Just to play around a little, I did this...

Open WordPad.
Add this to the document.
[tt]There is an [!]error[/!] with your data.[/tt]
Save the document as a Something.rtf

Then, open a VB project and place a rich text box control on a form.

In the properties window for the rich text box, you'll notice a filename property. Select the something.rtf file that you saved with wordpad.

I know this isn't completely what you are looking for. Many of the details are missing, but hopefully this will give you enough information to get you started.

Good luck.


-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Hi again George - once more, thank you: I've just this second cracked it myself. The correct syntax is:

EditWindow.TextRTF = Clipboard.GetText(&HBF01)

This drops in the contents of the clipboard, complete with the color formatting information that I wanted. I've a long way to go to do everything I want, but you've given me a sprint start.

Many, many thanks.
David
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top