c# - 表格,基本计算器按键触发问题
问题描述
private void UserInputText_KeyDown(object sender, KeyEventArgs e)
{
if ((e.KeyCode == Keys.D4 && e.Modifiers == Keys.Shift) || (e.KeyCode == Keys.Add))
{
if (String.IsNullOrEmpty(UserInputText.Text))
{
MessageBox.Show("Bir sayı giriniz.");
UserInputText.Clear();
return;
}
if (double.TryParse(UserInputText.Text, out sayı1))
{
CalculationResultText.Text = sayı1 + " + ";
islem = "+";
UserInputText.Clear();
}
else
{
MessageBox.Show("Sadece sayı değeri girebilirsiniz.");
UserInputText.Clear();
}
}
}
我正在编写一个基本表格计算器。当文本框聚焦并且用户按下“+”键时,我正在尝试触发添加功能并清除文本框。"if (String.IsNullOrEmpty(UserInputText.Text))
并且else
条件运行良好。但如果没有消息框显示在
if (double.TryParse(UserInputText.Text, out sayı1))
条件中,“+”字符将保留在文本框中,如图所示。感谢您的帮助。
解决方案
KeyPress
事件使您能够防止 TextBox 中的任何进一步更改。Handled
你可以做到这一点,这要归功于KeyPressEventArgs
private void UserInputText_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == '+')
{
UserInputText.Clear();
e.Handled = true;
}
}
推荐阅读
- reactjs - React useEffect 依赖于在调用 api 后初始化的值
- amazon-web-services - 当我们明确将 url 设置为 4576 时,localstack 尝试连接到 localhost:4566
- javascript - 动态选择要通过函数运行的数组
- javascript - 使用带有 e.preventDefault() 的提交按钮与常规按钮
- python - LDA:主题模型 gensim 给出相同的主题集
- c# - 在非阻塞for循环迭代中收集异常的最佳方法是什么
- bit-manipulation - 如何取消设置 N 最右边的设置位
- spring - 如何使用spring stomp websocket拒绝用户订阅或取消订阅服务器端的用户
- python - Scipy 最小化说它是成功的,然后继续警告
- c# - 如何动态更改 HybridWebView (iOS) Uri?