Prevent the ^ character from beeing entered in textbox.
-
How can I prevent the ^ character from being typed in a textbox? The problem seems to be that the character is not added before another keydown/keyup event. For some other keys I used the following code to prevent them from being typed:
void textBox_KeyUp(object sender, KeyEventArgs e)
{
if ((Keyboard.Modifiers & ModifierKeys.Alt) == ModifierKeys.Alt)
{
if (e.PlatformKeyCode <= 12)
e.Handled = true;switch (e.PlatformKeyCode) { case 226: e.Handled = true; break; } } else if ((Keyboard.Modifiers & ModifierKeys.Shift) == ModifierKeys.Shift) { switch (e.PlatformKeyCode) { case 52: e.Handled = true; break; case 186: e.Handled = true; break; } } }
However, this does not work for the ^ character. Thanks for help!
-
How can I prevent the ^ character from being typed in a textbox? The problem seems to be that the character is not added before another keydown/keyup event. For some other keys I used the following code to prevent them from being typed:
void textBox_KeyUp(object sender, KeyEventArgs e)
{
if ((Keyboard.Modifiers & ModifierKeys.Alt) == ModifierKeys.Alt)
{
if (e.PlatformKeyCode <= 12)
e.Handled = true;switch (e.PlatformKeyCode) { case 226: e.Handled = true; break; } } else if ((Keyboard.Modifiers & ModifierKeys.Shift) == ModifierKeys.Shift) { switch (e.PlatformKeyCode) { case 52: e.Handled = true; break; case 186: e.Handled = true; break; } } }
However, this does not work for the ^ character. Thanks for help!
-
How can I prevent the ^ character from being typed in a textbox? The problem seems to be that the character is not added before another keydown/keyup event. For some other keys I used the following code to prevent them from being typed:
void textBox_KeyUp(object sender, KeyEventArgs e)
{
if ((Keyboard.Modifiers & ModifierKeys.Alt) == ModifierKeys.Alt)
{
if (e.PlatformKeyCode <= 12)
e.Handled = true;switch (e.PlatformKeyCode) { case 226: e.Handled = true; break; } } else if ((Keyboard.Modifiers & ModifierKeys.Shift) == ModifierKeys.Shift) { switch (e.PlatformKeyCode) { case 52: e.Handled = true; break; case 186: e.Handled = true; break; } } }
However, this does not work for the ^ character. Thanks for help!
Couple of things: 1) KeyUp is probably not a good place to disable keys, as KeyPress happens repeatedly before KeyUp occurs. Try handling KeyDown, KeyPress and KeyUp with Console.WriteLine in each to show the flow of events. 2) If you are going to use magic numbers then comment the damn things so the next poor sod stands a chance of understanding what you are doing:
// Disable '%' key
case 52:is a lot more useful than
case 52:
By preference, use '%' instead of magic numbers, or constants with sensible names, or even "Keys." 3) Use KeyPress to disable your code:
private void textBox\_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar == '^') { e.Handled = true; } }
No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones
-
Couple of things: 1) KeyUp is probably not a good place to disable keys, as KeyPress happens repeatedly before KeyUp occurs. Try handling KeyDown, KeyPress and KeyUp with Console.WriteLine in each to show the flow of events. 2) If you are going to use magic numbers then comment the damn things so the next poor sod stands a chance of understanding what you are doing:
// Disable '%' key
case 52:is a lot more useful than
case 52:
By preference, use '%' instead of magic numbers, or constants with sensible names, or even "Keys." 3) Use KeyPress to disable your code:
private void textBox\_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar == '^') { e.Handled = true; } }
No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones
-
Thanks for the replies guys. Sorry, but I forgot to mention that I work in Silverlight. I can't get to the KeyPress event. Only KeyDown and KeyUp. Thanks again.
In that case you should probably post your question in the SilverLight Forum.
Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”