Click here to Skip to main content
15,889,795 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
C#
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();
        }



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

C#
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++;


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...
Posted

1 solution

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
C#
string keycode = e.KeyCode.ToString();
 
if (keycode == lettersInText[i].ToString().ToUpper())
{
...

To make it simpler you can store the whole initial text using upper case in the Load event if you like.
 
Share this answer
 
Comments
Nilesh Dalvi 19-Jul-15 15:33pm    
I am new in .net technology ...
Please clarify me what you meant to say "If you need to know is the letter in upper case you should investigae the shift property."

Being all letter in UPPERCASE in not an good idea...
Because I wants to create one sample typing tutor Software that will help user to familiar with keyboard's key..

If I do all letter in UPPERCASE then it will be some problems.
Wendelius 19-Jul-15 15:36pm    
The sample code I wrote doesn't expect the user to use uppercase. It makes the comparison in upper case so both upper and lower case are valid inputs.

Try modifying your code like the example in my solution and try it. Is it what you were after?
Nilesh Dalvi 19-Jul-15 15:42pm    
Thnx for your response..
See... I am trying all this with my Local Language[i.e. Mumbai,Maharashtra, India] OR in my mother Tongue[i.e. Marathi]

Why I am avoiding to accept UPPERCASE is because ... If I type something in Marathi letter in all UPPERCASE it will mean to different thing...

In Marathi we have to type UPPERCASE+LOWERCASE...

Hopes you got my point.
Wendelius 19-Jul-15 15:45pm    
Okay so you need to separate upper and lower case characters. In that case, in your code, check what is the value of the Shift property of the KeyEventArgs (e). Have a look at KeyEventArgs.Shift Property[^].

Hope that helps.
Nilesh Dalvi 19-Jul-15 16:03pm    
I did something like this... and it's working fine for me

private void richTextBox1_KeyPress(object sender, KeyPressEventArgs e)
{
string keycode = e.KeyChar.ToString();
if (keycode == lettersInText[i].ToString())
{
_letters = _letters + keycode;
richTextBox1.Select(0, _letters.Length);
richTextBox1.SelectionColor = Color.Red;
richTextBox1.Select(richTextBox1.TextLength, 0);
richTextBox1.SelectionColor = richTextBox1.ForeColor;
}
i++;
e.Handled = true;
}

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