Click here to Skip to main content
15,794,475 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How can I use different textcolours in a textblock with WPF. I like to use Visual Basic code for adding text to the textblock

What I have tried:

I have found the following code in C# but I want to do this in Visual Basic:

tb.Inlines.Add(new Run("text formatting ") { Foreground = Brushes.Blue });
Updated 23-Jan-22 5:10am

1 solution

You can do this:

<TextBlock Foreground="Black">This is normal text. <TextBlock Foreground="Red">This is red text.</TextBlock> This is more normal text.</TextBlock>

If you want to do it the way you indicated (in code-behind), you can use any free online code converter to find out how to do it in VB.Net.
Share this answer
Antoon Crama 23-Jan-22 21:13pm    
I have tried several code converters, but they do not convert the code. Apperently the converters have a problem with 'inlines'.
#realJSOP 24-Jan-22 6:37am    
I know this is going to sound like a radical concept, but you could try actually typing it into your source file. Intellisense should guide you. Try this:

tb.Inlines.Add(New Run("some text") With { .Foreground = Brushes.Blue })

Caveat - I don't do VB, so this is just a guess based on past exposure to the language (10 years ago).
Antoon Crama 29-Jan-22 20:48pm    
Thanks for the suggestion, but it did not work.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900