VB.NET tutorial: Make different colors to text


Make different colors to text

To change colors of the English characters and Khmer characters in the rich textbox, you need a highlight() procedure as shown below:

 Sub highlight()
On Error Resume Next
Dim c() As Char
c = Txtresult.Text.ToCharArray
'coloring v.,a., and n.
For i = 1 To c.Length - 2
If (c(i) = "." And c(i - 1) = "v") Or (c(i) = "." And c(i - 1) = "a") Or (c(i) = "." And c(i - 1) = "n") Then
Txtresult.SelectionStart = i - 1
Txtresult.SelectionLength = 2
Txtresult.SelectionColor = Color.Red

End If

'coloring adj., adv.
For i = 3 To c.Length - 1
If (c(i) = "." And c(i - 1) = "j" And c(i - 2) = "d") Or (c(i) = "." And c(i - 1) = "g" And c(i - 2) = "i") Or (c(i) = "." And c(i - 1) = "v" And c(i - 2) = "d") Then
Txtresult.SelectionStart = i - 3
Txtresult.SelectionLength = 4
Txtresult.SelectionColor = Color.Red
End If

'coloring Khmer character
For i = 0 To c.Length - 1
If Asc(c(i)) = 63 Then
txtresult.SelectionStart = i
txtresult.SelectionLength = 1
txtresult.SelectionColor = Color.Black

End If


End Sub

By using the above code, the English text such as v., a., n., adj., and adv. will be in red color and the Khmer characters will be in black color. The rest of the text in the rich textbox is MenuHighlight color style.


Dara comment


This example dictionary program is written in VS 2008. So there is no reason that this code does not work. What you have to check are:
1. Textresult must be the RichTextBox control,
2. There are characters, or words to be highlighted in the Textresult box.

vandy comment


sorry teacher, on Txtresult.SelectionColor = Color.Red .
why 'SelectionColor' can't write in my Visual Studio 2008?


This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.