1

我是 C# 的新手。我做了一个简单的计算器。但它没有键盘输入。如何启用它?

  1. 当从键盘按下数字 5 时,我想在文本框中得到 5 。

  2. 我也想在文本字段中隐藏鼠标光标。现在,当应用程序启动时,鼠标光标会出现在文本字段中。

这是我的代码:

namespace MyCalculator
{
    public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        //textBox1.Text = "0";


    }

    double num1=0, num2, result;
    string op;

    private void button_plus_Click(object sender, EventArgs e)
    {
        if (textBox1.Text != "") 
            num1 = Convert.ToDouble(textBox1.Text);

        textBox1.Text = String.Empty;
        op = "+";

    }

    private void button_minus_Click(object sender, EventArgs e)
    {
        if (textBox1.Text != "")
             num1 = Convert.ToDouble(textBox1.Text);

        textBox1.Text = String.Empty;
        op = "-";

    }

    private void button_mul_Click(object sender, EventArgs e)
    {
        if (textBox1.Text != "") 
              num1 = Convert.ToDouble(textBox1.Text);

        textBox1.Text = String.Empty;
        op = "*";

    }

    private void button_div_Click(object sender, EventArgs e)
    {
        if (textBox1.Text != "") 
                 num1 = Convert.ToDouble(textBox1.Text);

        textBox1.Text = String.Empty;
        op = "/";

    }

    private void button1_Click(object sender, EventArgs e)
    {
        textBox1.Text = textBox1.Text + "1";
    }

    private void button2_Click(object sender, EventArgs e)
    {
        textBox1.Text = textBox1.Text + "2";
    }

    private void button3_Click(object sender, EventArgs e)
    {
        textBox1.Text = textBox1.Text + "3";
    }

    private void button4_Click(object sender, EventArgs e)
    {
        textBox1.Text = textBox1.Text + "4";
    }

    private void button5_Click(object sender, EventArgs e)
    {
        textBox1.Text = textBox1.Text + "5";
    }

    private void button6_Click(object sender, EventArgs e)
    {
        textBox1.Text = textBox1.Text + "6";
    }

    private void button7_Click(object sender, EventArgs e)
    {
        textBox1.Text = textBox1.Text + "7";
    }

    private void button8_Click(object sender, EventArgs e)
    {
        textBox1.Text = textBox1.Text + "8";
    }

    private void button9_Click(object sender, EventArgs e)
    {
        textBox1.Text = textBox1.Text + "9";
    }

    private void button0_Click(object sender, EventArgs e)
    {
        textBox1.Text = textBox1.Text + "0";
    }

    private void button00_Click(object sender, EventArgs e)
    {
        textBox1.Text = textBox1.Text + "00";
    }

    private void button_point_Click(object sender, EventArgs e)
    {
        textBox1.Text = textBox1.Text + ".";
    }

    private void button_clear_Click(object sender, EventArgs e)
    {
        textBox1.Text = String.Empty;
    }

    private void button_result_Click(object sender, EventArgs e)
    {
        calculate(op);

    }

    public void calculate( string op)
    {
        num2 = Convert.ToDouble(textBox1.Text);
        switch(op)
        {
            case "+" : result=num1+num2;
                textBox1.Text = result.ToString(); break;
            case "-": result = num1 - num2;
                textBox1.Text = result.ToString(); break;
            case "*": result = num1 * num2;
                textBox1.Text = result.ToString(); break;
            case "/": result = num1 / num2;
                textBox1.Text = result.ToString(); break;

        }
        num1 = 0; num2 = 0;            
    }

    private void textBox1_TextChanged(object sender, EventArgs e)
    {

        textBox1.Font = new Font("Arial",12, FontStyle.Bold);
        textBox1.Cursor = Cursors.Arrow;


    }


}

}

4

1 回答 1

3

要捕获键盘KeyDown事件,您必须首先启用KeyPreview该表单。选择表单并转到属性并设置KeyPreview = true

使用KeyDown事件捕获键盘事件,

  private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode.Equals(Keys.NumPad5))
            {
               //Assuming button5 will set the value 5
               button5.PerformClick();
            }
        }

Hiding Cursor

我假设您期待与 Windows 计算器相同的行为。您可以禁用文本框,而不是简单地隐藏光标。设置文本框Enabled = false

于 2013-09-30T09:12:01.680 回答