如何使用文本框或Richtextbox为特定字符着色 [英] How to color the specific character using text box or Richtextbox
问题描述
我在 中找到偶数和奇数C#windows使用文本框/富文本框形成应用程序。
I am finding the even and odd number in C# windows form application using text box/rich text box.
out put应该像
Out put should be like
第一个方框:1 2 3 4 5 6
1st box: 1 2 3 4 5 6
2nd box:1 2 3 4 5 6 - 只有偶数才会变色。
2nd box: 1 2 3 4 5 6 - only even number should get colored.
看起来很简单但无法得到解决方案。
Looks simple but am unable to get solution.
可以帮助解决问题。
Can help to get the solution.
推荐答案
我在使用文本框/富文本框的C#windows窗体应用程序中找到偶数和奇数。
I am finding the even and odd number in C# windows form application using text box/rich text box.
Out put应该像
Out put should be like
第一个方框:1 2 3 4 5 6
1st box: 1 2 3 4 5 6
第二个方框:1 2 3 4 5 6 - 只有偶数才会变色。
2nd box: 1 2 3 4 5 6 - only even number should get colored.
private void button1_Click(object sender, EventArgs e)
{
richTextBox1.Text = textBox1.Text;
for(int n = 0; n < richTextBox1.Text.Length; ++n)
{
int temp = 0;
Int32.TryParse(richTextBox1.Text[n].ToString(), out temp);
if (temp % 2 == 0)
{
richTextBox1.Select(n, 1);
richTextBox1.SelectionColor = Color.Red;
}
}
richTextBox1.Select(0, 0);
}
构建并运行。
在TextBox中输入:
1 2 3 4 5 6
$
按下按钮。
你应该看到这个:
$
Build and run.
In the TextBox enter:
1 2 3 4 5 6
Press the button.
You should see this:
这篇关于如何使用文本框或Richtextbox为特定字符着色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!