C# tutorial: Dictionary-Make different colors to text


Dictionary: Make different colors to text



Change colors of the English characters and Khmer translation characters

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

private void highlight()
{

try
{
char[] c = Txtresult.Text.ToString().ToCharArray();
//coloring v.,a., and n. in red
int i;
for (i = 1; i <= Txtresult.Text.ToString().Length - 2; i++)
{
if ((c[i] == '.' && c[i - 1] == 'v') || (c[i] == '.' && c[1 - 1] == 'a') || (c[i] == '.' && c[i - 1] == 'n'))
//If (((c[i] == ".") && (c[i - 1] == "v")) || ((c[i] == ".") && (c[i - 1] == "a")) || ((c[i] == ".") && (c[i - 1] =="n")))
{
Txtresult.SelectionStart = i - 1;
Txtresult.SelectionLength = 2;
Txtresult.SelectionColor = Color.Red;

}

}
//coloring adj., adv. in red
for (i = 3; i <= Txtresult.Text.ToString().Length - 1; i++)
{


if ((c[i] == '.' && c[i - 1] == 'j' && c[i - 2] == 'd') || (c[i] == '.' && c[i - 1] == 'g' && c[i - 2] == 'i') || (c[i] == '.' && c[i - 1] == 'v' && c[i - 2] == 'd'))
{
Txtresult.SelectionStart = i - 3;
Txtresult.SelectionLength = 4;
Txtresult.SelectionColor = Color.Red;
}

}
//coloring Khmer characters in black color

for (i = 0; i <= Txtresult.Text.ToString().Length - 1; i++)
{

if ((int)c[i] > 127)
{

Txtresult.SelectionStart = i;
Txtresult.SelectionLength = 1;
Txtresult.SelectionColor = Color.Black;

}

}


}
catch (Exception ex) { }

}

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.






Comments

CAPTCHA image



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.