我需要%,&,/,",'从文本框中排除特殊字符(等)
可能吗?我应该使用key_press事件吗?
string one = radTextBoxControl1.Text.Replace("/", "");
string two = one.Replace("%", "");
//more string
radTextBoxControl1.Text = two;
Run Code Online (Sandbox Code Playgroud)
在这种模式下非常长=(
Hos*_*dir 15
我假设你试图只保留字母数字和空格字符.像这样添加一个按键事件
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
var regex = new Regex(@"[^a-zA-Z0-9\s]");
if (regex.IsMatch(e.KeyChar.ToString()))
{
e.Handled = true;
}
}
Run Code Online (Sandbox Code Playgroud)
下面的代码只允许数字、字母、退格键和空格。
我包含了 VB.net,因为我必须处理一个棘手的转换。
C#
private void textBoxSample_KeyPress(object sender, KeyPressEventArgs e)
{
e.Handled = e.KeyChar != (char)Keys.Back && !char.IsSeparator(e.KeyChar) && !char.IsLetter(e.KeyChar) && !char.IsDigit(e.KeyChar);
}
Run Code Online (Sandbox Code Playgroud)
VB网
Private Sub textBoxSample_KeyPress(sender As Object, e As KeyPressEventArgs) Handles textBoxSample.KeyPress
e.Handled = e.KeyChar <> ChrW(Keys.Back) And Not Char.IsSeparator(e.KeyChar) And Not Char.IsLetter(e.KeyChar) And Not Char.IsDigit(e.KeyChar)
End Sub
Run Code Online (Sandbox Code Playgroud)
小智 5
你可以使用这个:
private void textBoxSample_KeyPress(object sender, KeyPressEventArgs e)
{
e.Handled = !char.IsLetter(e.KeyChar) && !char.IsDigit(e.KeyChar);
}
Run Code Online (Sandbox Code Playgroud)
它阻止特殊字符并且只接受整数/数字和字符
| 归档时间: |
|
| 查看次数: |
60318 次 |
| 最近记录: |