Private Sub TextBoxSend_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBoxSend.KeyPress If CheckBoxHexSend.Checked = True Then '文本框防止非法字符输入: '只输入整数和A-F:及 空格 '3 - ctrl + c 复制 '22 - ctrl + v 粘贴 '24 - ctrl + x 剪切 '13 回车键 '17 控制键,允许复制 '8 退格键 '32 空格键 Dim KeyAscii As Short = Asc(e.KeyChar) Select Case KeyAscii Case 13, 32, 8, Asc("0") To Asc("9"), Asc("A") To Asc("F"), Asc("a") To Asc("f") '加回车键 ' e.KeyChar = Chr(KeyAscii) Case Else KeyAscii = 0 '按键都屏蔽 End Select If KeyAscii = 0 Then e.Handled = True End If End If End Sub‘类似这样改吧,已经说得很清楚啦
'文本框防止非法字符输入:
'只输入整数和A-F:及 空格
'3 - ctrl + c 复制
'22 - ctrl + v 粘贴
'24 - ctrl + x 剪切
'13 回车键
'17 控制键,允许复制
'8 退格键
'32 空格键
Dim KeyAscii As Short = Asc(e.KeyChar) Select Case KeyAscii
Case 13, 32, 8, Asc("0") To Asc("9"), Asc("A") To Asc("F"), Asc("a") To Asc("f") '加回车键
' e.KeyChar = Chr(KeyAscii)
Case Else
KeyAscii = 0 '按键都屏蔽
End Select
If KeyAscii = 0 Then
e.Handled = True
End If End If End Sub‘类似这样改吧,已经说得很清楚啦
if (47 < e.KeyValue && e.KeyValue < 58)
{
if (e.Shift)
{
if(e.KeyCode==Keys.D2)
KeyPressValue += "@";
}
else
KeyPressValue += e.KeyCode.ToString().Replace("D", "");
}判断键入的键盘Value值,数字键0-9对应的ascii码是48-57
http://www.developerfusion.com/tools/convert/vb-to-csharp/
这是改成C#的网站
private void TextBoxSend_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if (CheckBoxHexSend.Checked == true) {
//文本框防止非法字符输入:
//只输入整数和A-F:及 空格
short KeyAscii = Strings.Asc(e.KeyChar);
switch (KeyAscii) {
case 13:
case 32:
case 8:
case Strings.Asc("0"): // TODO: to Strings.Asc("9")
case Strings.Asc("A"): // TODO: to Strings.Asc("F")
case Strings.Asc("a"): // TODO: to Strings.Asc("f")
//加回车键
break;
// e.KeyChar = Chr(KeyAscii)
default:
KeyAscii = 0;
//按键都屏蔽
break;
}
if (KeyAscii == 0) {
e.Handled = true;
}
}
}
//类似这样改吧,已经说得很清楚啦--不好意思新手练习发源代码-实验下
<ajaxToolkit:FilteredTextBoxExtender ID="ftbe" runat="server" TargetControlID="TextBox" FilterType="Custom, Numbers" ValidChars="+-*/" />
/// <summary>
/// 判断是否为纯数字
/// </summary>
/// <param name="txt"></param>
/// <returns></returns>
public static bool isNumornot(string txt)
{
if (Regex.Match(txt, "^[0-9]+$").Success)
{
return true;
}
else
{
return false;
}
}
onKeyUp="this.value=this.value.replace(/\D/g,'')" onafterpaste="this.value=this.value.replace(/\D/g,'')"