KeyPress事件

时间:2023-03-09 07:28:40
KeyPress事件

在做一个小demo的时候,发现在文本框中输入一个数字,按下“+”,数字增加了,但是“+”仍旧存在的问题,解决方案:提前执行键盘press事件

private void txtNum_KeyPress(object sender, KeyPressEventArgs e)
{
//string str = "";
//if (e.KeyChar == 43)
//{
// this.Focus();
// str = this.txtNum.Text.Trim();
// txtNum.Text = (Convert.ToInt32(str) + 1).ToString();
// if (txtNum.Text.Contains("+"))
// {
// txtNum.Text = txtNum.Text.Replace("+", "");
// }
//}
if (e.KeyChar == '+')
{
int num = ;
int.TryParse(txtNum.Text, out num);
txtNum.Text = (++num).ToString();
txtNum.SelectionStart = txtNum.Text.Length;
e.Handled = true;//加入此行,“+”不显示
}
}