如何在WinForm中突出显示RichTextBox文本 [英] How do I highlight RichTextBox Text in WinForm

查看:93
本文介绍了如何在WinForm中突出显示RichTextBox文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨...

我在表单上只添加了一个RichTextBox,文字说嗨..我是CodeProject。



所以,

我想做的是......



专注于RichTextBox ..用户将键入相同的字母,如图所示RichTextBox的。如果用户输入的字母& RichTextBox字母匹配它将突出显示它的文字...如果用户按错了关键字,它将显示一些警告信息。



我做了类似这样的事情......但是工作不正常。



首先我将所有字母保存在数组中

Hi...
I added only one RichTextBox on form having text say like "Hi.. I am CodeProject."

So,
what I wants to do is...

focusing on RichTextBox.. user will type the letter same as shown in RichTextBox. If user's typed letter & RichTextBox letter match it will Highlight it's text letter by letter... and if user press wrong key letter it will show some alert message.

I did something like this... but it not working properly.

first of all I saved all letter in Array

string[] lettersInText;

        private void Form1_Load(object sender, EventArgs e)
        {
            lettersInText = new string[richTextBox1.TextLength];

            for (int i = 0; i < richTextBox1.TextLength; i++)
            {
                AllLetterInTextBox = richTextBox1.Text;
                FirstLetterInTextBox = AllLetterInTextBox[i].ToString();
                lettersInText[i] = FirstLetterInTextBox;
            }
            richTextBox1.Focus();
        }







然后尝试将所有保存在Array中的字母与e匹配RichTextBox的KeyDown事件上的.KeyCode






and then trying to match all that letter which saved in Array with e.KeyCode on KeyDown event of RichTextBox

string keycode = e.KeyCode.ToString();

if (keycode == lettersInText[i].ToString())
{
    _letters = _letters + e.KeyCode.ToString();
    richTextBox1.Select(0, _letters.Length);
    richTextBox1.SelectionColor = Color.Red;
    richTextBox1.Select(richTextBox1.TextLength, 0);
    richTextBox1.SelectionColor = richTextBox1.ForeColor;
}
i++;





我能说;它说工作正常......但只有问题是e .KeyDown只显示大写字母......即使我按下小写字母。





请帮帮我...



I can;t say it working fine ... but only problem is e.KeyDown showing only UPPERCASE letter... even I press lowercase.


Help me out please...

推荐答案

如果我理解你的问题,KeyDown事件中的比较只适用于大写输入。



如果是例如,大写和小写字符的键码相同。如果您需要知道大写字母,您应该调查shift属性。但是,如果您希望接受大写和小写字符,则以大写形式进行比较。



类似

If I understand your question correctly, the comparison in KeyDown event works only with uppercase input.

If that is the case, the keycode is the same for upper and lower case characters. If you need to know is the letter in upper case you should investigae the shift property. However, if you want both upper and lower case characters to be accepted then make the comparison in upper case.

Something like
string keycode = e.KeyCode.ToString();
 
if (keycode == lettersInText[i].ToString().ToUpper())
{
...



为简化起见,如果愿意,可以在Load事件中使用大写字母存储整个初始文本。


To make it simpler you can store the whole initial text using upper case in the Load event if you like.


这篇关于如何在WinForm中突出显示RichTextBox文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆