Changing font colors of concatenated strings

  • Thread starter Thread starter JoelW
  • Start date Start date
J

JoelW

Let's say you're programming in VB.NET, and you have:

Dim str1 As String
Dim str2 As String
Dim str3 As String


str1 = "I am red"
str2 = "I am yellow"
str3 = "I am dark green"

With TextBox1
.Text = str1 & ", " & str2 & ", " & str3
.SelectAll
.Copy
End With


....and you want to copy 'str' to the clipboard, with "I am red" in red
font, "I am yellow" in yellow font, and "I am dark green" in dark green
font?
I feel like I'm missing something obvious...


Thanks!
 
JoelW said:
Let's say you're programming in VB.NET, and you have:

Dim str1 As String
Dim str2 As String
Dim str3 As String


str1 = "I am red"
str2 = "I am yellow"
str3 = "I am dark green"

With TextBox1
.Text = str1 & ", " & str2 & ", " & str3
.SelectAll
.Copy
End With


...and you want to copy 'str' to the clipboard, with "I am red" in red
font, "I am yellow" in yellow font, and "I am dark green" in dark green
font?
I feel like I'm missing something obvious...


Thanks!

You are copying text to the clipboard. The clipboard does not contain
anything for colors. It has no UI to display colors/strings. All it cares
about is that you are copying "data" to the clipboard to be used at a later
time. What you are copying from may display the strings in colors. For
example, if you are copying from a Rich Text Box, then when you copy the
text, you are copying the format (the Rich Text format) as well as the text.
The "data" is the format code which contains the text you are copying.

Hope this helps :)

Mythran
 
Then how do I get the different sections of the concatenated string to
show up as different colors in this instance?
 
JoelW said:
Then how do I get the different sections of the concatenated string to
show up as different colors in this instance?

It all depends on what you are coping to. If you sent data in html
format and the object you are pasting to accepts that format then it
will work. if you paste in Rich text format then the object you are
pasting to needs to accept that as well.

Chris
 
Hi,

The textbox does not support color. Use a richtextbox instead.
This code will place the text in the color you want in the richtextbox and
then copy it to the clipboard.

RichTextBox1.SelectionColor = Color.Red
RichTextBox1.SelectedText = "I am red" & ControlChars.NewLine
RichTextBox1.SelectionColor = Color.Yellow
RichTextBox1.SelectedText = "I am yellow" & ControlChars.NewLine
RichTextBox1.SelectionColor = Color.DarkGreen
RichTextBox1.SelectedText = "I am dark green" & ControlChars.NewLine
RichTextBox1.SelectAll()
RichTextBox1.Copy()


Ken
---------------------
Let's say you're programming in VB.NET, and you have:

Dim str1 As String
Dim str2 As String
Dim str3 As String


str1 = "I am red"
str2 = "I am yellow"
str3 = "I am dark green"

With TextBox1
.Text = str1 & ", " & str2 & ", " & str3
.SelectAll
.Copy
End With


....and you want to copy 'str' to the clipboard, with "I am red" in red
font, "I am yellow" in yellow font, and "I am dark green" in dark green
font?
I feel like I'm missing something obvious...


Thanks!
 
Back
Top