在KeyPress事件中做处理,如果输入的您不允许输入的内容,
e.Handled=false;
这样就可以了。我可以给你一段我做过的一个只能输入数值的TextBox控件的部分代码,原理和这个相似private void NumTextBox_KeyPress(object sender, KeyPressEventArgs e)
{
int KeyAscii;
KeyAscii = (int)e.KeyChar;
// 0 - 9 ,退格,回车
if ((KeyAscii >= 48 && KeyAscii <= 57) || KeyAscii == 8 || KeyAscii == 13)
{ }
else if(KeyAscii == 45) // - 负号的处理
{
if (this.Text.IndexOf("-") >= 0) { KeyAscii = 0; }
if (this.SelectionStart != 0) { KeyAscii =0; }
}
else if (KeyAscii == 46) // . 小数点的处理
{
if (this.Text.IndexOf(".") >= 0)
KeyAscii = 0;
}
else{ KeyAscii = 0; } if (KeyAscii==0)
e.Handled = true;
else
e.Handled = false;
}
e.Handled=false;
这样就可以了。我可以给你一段我做过的一个只能输入数值的TextBox控件的部分代码,原理和这个相似private void NumTextBox_KeyPress(object sender, KeyPressEventArgs e)
{
int KeyAscii;
KeyAscii = (int)e.KeyChar;
// 0 - 9 ,退格,回车
if ((KeyAscii >= 48 && KeyAscii <= 57) || KeyAscii == 8 || KeyAscii == 13)
{ }
else if(KeyAscii == 45) // - 负号的处理
{
if (this.Text.IndexOf("-") >= 0) { KeyAscii = 0; }
if (this.SelectionStart != 0) { KeyAscii =0; }
}
else if (KeyAscii == 46) // . 小数点的处理
{
if (this.Text.IndexOf(".") >= 0)
KeyAscii = 0;
}
else{ KeyAscii = 0; } if (KeyAscii==0)
e.Handled = true;
else
e.Handled = false;
}
private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
string s="一二三四五六七八九十零";
if(s.IndexOf(e.KeyChar)==-1)
{
e.Handled=true;
this.Focus();//如果是form是MDI的子窗体,将this改成MDI窗体实例
}
}
frm_load()
'在frmload时写
old=textbox2.text
end sub
Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChangedif not (textbox2.text.replace(oldstr,"") in "一二三四")
textbox2.text=oldstr
end ifoldstr=textbox2.text
End Sub
Key_Press事件和Key_Changed()事件都 可以。
Focus获得输入焦点
KeyPressEventArgs.Handled
来标释是否处理KeyPress事件
{
string str = "";
foreach(char c in this.Text)
{
if(Char.IsDigit(c))
{
str += c.ToString();
}
}
this.Text = str;
}
protected override void OnKeyDown(KeyEventArgs e)
{
if((e.KeyCode ==Keys.V)&&(e.Control==true))
{
e.Handled = true;
}
else
{
base.OnKeyDown(e);
}
}
protected override void OnMouseDown(MouseEventArgs e)
{
if(e.Button !=MouseButtons.Right)
{
base.OnMouseDown(e);
}
}
}
你看看也许对你有用!
web的怎么办?