TextBox – TextChanged事件Windows C#
发布时间:2020-12-14 05:45:39 所属栏目:Windows 来源:网络整理
导读:我陷入困境,需要投入.这是描述 – 我在Windows窗体C#中有一个txtPenaltyDays private void txtPenaltyDays_TextChanged(object sender,EventArgs e){ if(Convert.ToInt16(txtPenaltyDays.Text) 5) { MessageBox.Show("The maximum amount in text box cant b
我陷入困境,需要投入.这是描述 –
我在Windows窗体C#中有一个txtPenaltyDays private void txtPenaltyDays_TextChanged(object sender,EventArgs e) { if(Convert.ToInt16(txtPenaltyDays.Text) > 5) { MessageBox.Show("The maximum amount in text box cant be more than 5"); txtPenaltyDays.Text = 0;// Re- triggers the TextChanged } } 但是我遇到了问题,因为这会引发2次.因为将文本值设置为0. 任何建议都深表感谢. 解决方法
只需在发现无效值时禁用事件处理程序,通知用户然后重新启用事件处理程序
private void txtPenaltyDays_TextChanged(object sender,EventArgs e) { short num; if(Int16.TryParse(txtPenaltyDays.Text,out num)) { if(num > 5) { txtPenaltyDays.TextChanged -= txtPenaltyDays_TextChanged; MessageBox.Show("The maximum amount in text box cant be more than 5"); txtPenaltyDays.Text = "0";// txtPenaltyDays.TextChanged += txtPenaltyDays_TextChanged; } } else { txtPenaltyDays.TextChanged -= txtPenaltyDays_TextChanged; MessageBox.Show("Typed an invalid character- Only numbers allowed"); txtPenaltyDays.Text = "0"; txtPenaltyDays.TextChanged += txtPenaltyDays_TextChanged; } } 另请注意,我已删除Convert.ToInt16,因为如果您的用户键入字母而不是数字并使用Int16.TryParse,则会失败 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- windows – PermissionError:[Errno 13]权限被拒绝
- 如何在Windows中没有IDE的情况下开发C程序?
- Windows – 64位操作系统上的32位应用程序
- Windows – 如何找到我的CreateFont调用实际使用的字体?
- windows – AD:你能成为无限团体的成员吗?
- 使用puppet exec检测是否已安装Windows服务
- winapi – 有没有办法找出win32信号量的当前计数?
- Soap UI – Windows中存储的首选项/设置
- windows-server-2008 – 使用perfmon监控服务
- windows-phone-7 – 如何在Windows Phone 7上安装根证书?