Note that there are some explanatory texts on larger screens.

plurals
  1. POSIP: Key.Shift not getting detected
    primarykey
    data
    text
    <p>I am trying to create a text box which accepts only alphabets and numbers, and discards all the special characters. I need this for entering a valid file name, without extension. I am using the following code:</p> <pre><code>private void txtFName_KeyDown(object sender, KeyEventArgs e) { if (!(((e.Key &gt;= Key.A) &amp;&amp; (e.Key &lt;= Key.Z)) || ((e.Key &gt;= Key.D0) &amp;&amp; (e.Key &lt;= Key.D9)) || (e.Key == Key.Shift) || (e.Key == Key.Back))) { e.Handled = true; MessageBox.Show("No Special Characters are Allowed!"); } } </code></pre> <p>Unfortunately the "KeyPress" event is not there, so I figured out that this is the best way to achieve what I am doing.</p> <p>The problem I am facing is:</p> <p>As you can see in the code above, I have taken care of the "Shift" key press, but when I press the "Shift" key on the SIP, the "No Special Characters are Allowed" Message box pops up 3 times before I can key in an upper case alphabet!!! So this essentially prevents me from entering any Upper case characters. </p> <p>Worse still, it is accepting all the characters !@#$%^&amp;*(). Probably because it is detecting these as numbers from 0-9. It looks that the key codes are being returned the same for 2 and @, 3 and # and so on. This is very strange behavior! And I can not even use the underscore key with the above technique. </p> <p>Such behavior is obviously not acceptable in a professional App.</p> <p>How can I create a text box which accepts only alphabets, numbers and underscore, and discards all other characters? Also, is there a problem with the "Shift" key not getting detected?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload