简体   繁体   English

监视击键

[英]Monitor keystrokes

I am a beginner in C# , and making a calculator . 我是C#的初学者,并且正在制作一个计算器。 But i want to disable the "GO!" 但我想禁用“GO!” button when there's no number typed in textbox1 and once the user enters a number in it the "GO!" 按钮,当textbox1中没有输入数字时,一旦用户在其中输入数字“GO!” button becomes enabled again ... how to do this in C# ? 按钮再次启用...如何在C#中执行此操作? i tried KeyDown and KeyPress event like this but never worked 我尝试过这样的KeyDown和KeyPress事件,但从未奏效

private void Form1_Load(object sender, EventArgs e)
    {

            button15.Enabled = false;
         button16.Enabled = false;     

    }


 private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyEventArgs e)
    {

       if (e.KeyCode < Keys.D0 || e.KeyCode > Keys.D9)
        {
            button15.Enabled = true;
            button16.Enabled = true;

        }
        else
        {
            button15.Enabled = false;
            button16.Enabled = false;
        }

    } 

so how to do this please ? 那怎么办呢? thanks in advance . 提前致谢 。

Your idea is pretty good but there is a better way. 你的想法非常好,但还有更好的方法。 Just detect when the contents of the text box change . 只需检测文本框内容何时发生变化 When the contents change, check to see whether the contents are empty or not empty. 当内容改变时,检查内容是否为空或不为空。 If the contents are empty then disable the button, otherwise enable it. 如果内容为空,则禁用该按钮,否则启用它。

http://msdn.microsoft.com/en-us/library/system.windows.forms.control.textchanged.aspx http://msdn.microsoft.com/en-us/library/system.windows.forms.control.textchanged.aspx

Also, it would be a good idea for you to change the names of your controls to something other than the defaults. 此外,最好将控件名称更改为默认值以外的其他名称。 "button15" tells the reader of the code nothing, "goButton" does. “button15”告诉读者没有代码,“goButton”。

private void textBox1_TextChanged(object sender, EventArgs e)
{
      if(String.IsNullOrEmpty(textBox1.Text))
      {
            button15.Enabled = false;
            button16.Enabled = false;
      }
      else
      {
            button15.Enabled = true;
            button16.Enabled = true;
      }
}

You'll want to check if the text box value can actually be converted to a number. 您需要检查文本框值是否可以实际转换为数字。 Checking for digits isn't good enough, you'll accept something bad like "4-" or reject something good like "1e3". 检查数字是不够好的,你会接受像“4”这样的坏事或拒绝像“1e3”这样的好事。 And please use meaningful control names so you don't accidentally enable the wrong control. 请使用有意义的控件名称,这样您就不会意外启用错误的控件。 For example: 例如:

private void txtOperand_TextChanged(object sender, EventArgs e) {
  double value;
  btnGo.Enabled = double.TryParse(txtOperand.Text, out value);
}

This probably isn't quite good enough for your calculator, but demonstrates the approach. 对于您的计算器来说,这可能不够好,但演示了这种方法。

How about using the TextChanged event, and then checking if the textbox is empty or has something in it and enabling or disabling the button accordingly? 如何使用TextChanged事件,然后检查文本框是否为空或其中有什么内容并相应地启用或禁用该按钮?

Also, if you were using WPF, you could write a command for your GO button which would automagically handle activation and deactivation of the command (and any controls which subscribe to it). 此外,如果您使用的是WPF,则可以为GO按钮编写一个命令,该命令将自动处理命令的激活和取消激活(以及任何订阅它的控件)。 Your activation logic would be simply to check if there's text in the textbox. 您的激活逻辑只是检查文本框中是否有文本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM