我用“+”和回车键作为KeyDown事件的响应键,分别执行不同的功能,当在bianhao文本框中输入“+”时,执行代码后将bianhao清空,但我用下面的代码时bianhao文本框中保留了“+”,而没有被完全清空,请教这问题该怎么解决?
private void bianhao_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
if (e.KeyCode == Keys.Add)
{
……
bianhao.Text = "";
}
private void bianhao_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
if (e.KeyCode == Keys.Add)
{
……
bianhao.Text = "";
}
this.bianhao.KeyPress += new System.Windows.Forms.KeyEventHandler(this.bianhao_KeyPress);
…
private void bianhao_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
private void textBox1_TextChanged(object sender, EventArgs e)
{
string str = this.textBox1.Text;
if (str.Length > 0)
{
char[] c = str.ToCharArray(str.Length - 1, 1);
//char c = str.Substring(str.Length - 2, 1);
if (c[0] == '+')
{
this.textBox1.Text = "";
}
}
}可以的
bianhao.Text = "";
之后加上
e.Handled = true;
即可
public class MyTextBox:TextBox
{
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if( keyData == Keys.Add )
{
this.Text = "";
return true;
}
return base.ProcessCmdKey (ref msg, keyData);
}
}
private void textBox3_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if (e.KeyChar =='+')
{
this.textBox3.Text = "";
e.Handled=true;
}
}