RE: Textbox, keypress keychar, non ASCII characters

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Hi kenneth,

Thanks for your feedback.

For this request, I think we can first get the text change
notification(with OnTextChanged method or TextChanged event), then in this
method/event, just determine if the modified text content string is ascii
characters string.

I think the key point is the last step: how to determine a string is ascii
character string. Normally, we can use ASCIIEncoding.GetBytes() method to
get the byte array of the string, when if certain character is Unicode
character and can not be parse as ascii code, it will become '?'(which is
0x3f) character in the result byte array. So we can just determine if the
result byte array contains any 0x3f value. If not, the input is valid,
else, the we can reset the content back to its original value. Sample code
like this:

public class MyTextBox : System.Windows.Forms.TextBox
{
private string OriginalText=string.Empty;
protected override void OnTextChanged(EventArgs e)
{
base.OnTextChanged (e);

byte[] newtextbytes=ASCIIEncoding.ASCII.GetBytes(this.Text);
for(int i=0;i<newtextbytes.Length;i++)
{
if(newtextbytes[i]==0x3f)
{
this.Text=OriginalText;
return;
}
}
OriginalText=this.Text;
}
}
This code snippet works well on my side, for your information.
=======================================
Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

.



Relevant Pages

  • Help in Spanish translation of the description of UDFs
    ... functions of minimum / maximum values among elements of an array column. ... GETALLWORDS- Inserts the words from a string into a global dimensioned ... WORDTRAN- Searches a character string for occurrences of a first word, ... ARRAYSUM- Returns the sum of all or a specified range of numeric (and/or ...
    (microsoft.public.fox.helpwanted)
  • Re: RfD: Escaped Strings
    ... the S" string can only contain printable characters, ... the S" string cannot contain the '"' character, ... \b BS (backspace, ASCII 8) ... \ ** escapes to characters much as C does. ...
    (comp.lang.forth)
  • RfD: Escaped Strings version 4
    ... the S" string can only contain printable characters, ... the S" string cannot contain the '"' character, ... as an escape character for the entry of characters that cannot be ... \b BS (backspace, ASCII 8) ...
    (comp.lang.forth)
  • RfD: Escaped Strings version 4
    ... the S" string can only contain printable characters, ... the S" string cannot contain the '"' character, ... as an escape character for the entry of characters that cannot be ... \b BS (backspace, ASCII 8) ...
    (comp.lang.forth)
  • easy parsing problem in C for beginner
    ... subscripted value is neither array nor pointer ... When the character is 0 ... ascii value of the character that represents it ... void s_Convert_Array(PA_PluginParameters params) ...
    (comp.sys.mac.programmer.help)