Click here to Skip to main content
15,890,506 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hi all,

I am using one RichTextBox in C# Windows Application. I need to restrict the user from entering any key from keyboard at certain conditions. so, I wrote the following line of code in keydown event of RichTextBox at .
C#
e.SuppressKeyPress = true;

This is working fine for English US keyboard. But when i changed my regional settings to Korea and keyboard language to Korean, i can enter the Korean characters (like 뮤아ㅓㅑㅇ) even after executing the above statement.

even i have tried with the following code
C#
e.SuppressKeyPress = true;
e.Handled = true;

but in vain.

Please help.
Thanks a lot in advance.
Posted
Updated 12-Feb-12 17:21pm
v4
Comments
Sergey Alexandrovich Kryukov 10-Feb-12 11:44am    
First of all, tag your UI library. WPF, Forms, Silverlight, ASP.NET, what? Probably this is "WinForms".

Are your sure e.SuppressKeyPress is executed in case of Korean character? Could you make a complete but short code sample manifesting this problem? It can help a lot.
--SA
Member 8636942 12-Feb-12 23:45pm    
Thanks for your comment. As i am new to c# i am not aware of all these things. (Anyway have added more information to the question) No doubt that e.SupressKeyPress is executed when typing Korean characters. All i did is : I have taken one richtextbox and handled keydown event like below.

private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
{
e.SuppressKeyPress = true;
e.Handled = true;
}


This code is working fine(supressing all the keys for English US keyboard). When i change my regional settings to Korea and my keyboard language to Korea. Even though the control passing through the statements, this code unable to supress the keys.

One more thing, when i typed korean characters, the keypress event is not firing (i have handled later to check). Many of the keys i typed giving me only one value "229" for the three keydata, keyvalue, keycode

Too bad you did not provide relevant information. It's probably System.Windows.Forms. Handle the event KeyPress or override the virtual method OnKeyPress and use KeyPressEventArgs.Handled to suppress the key.

Please see:
http://msdn.microsoft.com/en-us/library/system.windows.forms.keypresseventargs.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.aspx[^].

—SA
 
Share this answer
 
Comments
Espen Harlinn 11-Feb-12 5:11am    
5'ed!
Sergey Alexandrovich Kryukov 14-Feb-12 1:57am    
Thank you, Espen.
--SA
Have you tried handling the events for control keys ? Perhaps that's how the system views those keys. What you have done is correct, and should work. Do the events fire at all when you press those keys, have you set a breakpoint to see ?
 
Share this answer
 
Comments
Member 8636942 12-Feb-12 23:19pm    
I have tried to handle the control keys also. But while typing this korean characters, always the control value is false. (I checked e.Control value in keydown event). Also, i can confirm that when korean characters are types, the keydown event is firing also, the statement e.SupressKeyPress = true; is executing. still i can see the korean characeters in richtextbox.
ProEnggSoft 13-Feb-12 0:46am    
Have you tried my solution (solution No.3)?
You can use the KeyPress event to suppress the required keys.
The e.KeyChar property gives the unicode number of the character.
The following code snippet can be used to suppress the required keys.
C#
int MinKeyCode = 2309; // Enter the minimum key code here
int MaxKeyCode = 2320; // Enter the maximum key code here
private void richTextBox1_KeyPress(object sender, KeyPressEventArgs e) {

    if (e.KeyChar >= MinKeyCode && e.KeyChar <= MaxKeyCode)
    {
        e.Handled = true;
    }
}

You can use the following code to find out the unicode number of the characters you want to suppress

C#
private void richTextBox1_KeyPress(object sender, KeyPressEventArgs e) {
    listBox1.Items.Add(e.KeyChar + " - " + (int)e.KeyChar);
}
 
Share this answer
 
I tried with the following :

setting "Readonly" property of the richtextbox to true at selected conditions (where i supposed to suppress the key) and again making the "Readonly" property of the richtextbox to "false" in keyup event.


It meets the requirement. Now, it is working fine.

Many Thanks for your time spent on this.
 
Share this answer
 
v2

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