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ます。

event を使用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 に答える