在c#winform中怎么控制textbox输入的值

如题所述

键盘输入的字符可以通过重写TextBox控件的OnKeyPress()事件处理,见如下代码: protected override void OnKeyPress(KeyPressEventArgs e) // 屏蔽非数字键 { base.OnKeyPress(e); if (this.ReadOnly) // 只读, 不处理 return; if ((int)e.KeyChar <= 32) // 特殊键(含空格), 不处理 return; if (!char.IsDigit(e.KeyChar)) // 非数字键, 放弃该输入 { e.Handled = true; return; } }
温馨提示:答案为网友推荐,仅供参考
相似回答