亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

將kepypress事件添加到基本計算器表單

將kepypress事件添加到基本計算器表單

C#
RISEBY 2021-08-29 18:03:25
我在visual studio中制作了一個基本的計算器表格,它使用鼠標點擊事件功能齊全。我正在嘗試添加鍵盤按下事件,以便您可以使用鍵盤或鼠標,但無法弄清楚。我為 keypress 事件添加了一些代碼,但它只有在我先用鼠標單擊按鈕時才有效,然后鍵盤事件才會起作用,如果我用鼠標選擇另一個數字,它就會停止工作。//Code that handles click eventsprivate void number_Click(object sender, EventArgs e)    {        if((txtResult.Text == "0")||(operation))        {            txtResult.Clear();        }                   //Cast button to the object sender        operation = false;        Button button = (Button)sender;        //If the button pressed is a "." then check if the txtResult already contains        //a ".", if it does contain a "." then do nothing        if (button.Text == ".")        {            if (!txtResult.Text.Contains("."))            {                txtResult.Text = txtResult.Text + button.Text;            }        }else        txtResult.Text = txtResult.Text + button.Text;    }//Keyboard press event code    private void num1_key(object sender, KeyPressEventArgs e)    {        if (e.KeyChar == '1')        {//If keyboard '1' pressed perform number_click()            btn1.PerformClick();            e.Handled = true;        }    }有什么明顯的我遺漏了還是我走錯了路?
查看完整描述

1 回答

?
森林海

TA貢獻2011條經驗 獲得超2個贊

更新答案


由于您仍然有問題,您必須做的是刪除以前的代碼并使用以下代碼。


    //Have your form's KeyPress event like this, replace textBox1 with the name of your textbox

    private void Form1_KeyPress(object sender, KeyPressEventArgs e)

    {

        switch (e.KeyChar)

        {

            case '0':

                textBox1.Text += "0";

                break;


            case '1':

                textBox1.Text += "1";

                break;


            case '2':

                textBox1.Text += "2";

                break;


            case '3':

                textBox1.Text += "3";

                break;


            case '4':

                textBox1.Text += "4";

                break;


            case '5':

                textBox1.Text += "5";

                break;


            case '6':

                textBox1.Text += "6";

                break;


            case '7':

                textBox1.Text += "7";

                break;


            case '8':

                textBox1.Text += "8";

                break;


            case '9':

                textBox1.Text += "9";

                break;

            case '.':

                textBox1.Text += ".";

                break;

        }


        e.Handled = true;

    }

有這樣的表單加載事件。


    private void Form1_Load(object sender, EventArgs e)

    {

        this.KeyPreview = true;

    }

舊答案


您需要做的是在您的表單上創建 KeyPress 事件,如下所示。


private void Form1_KeyPress(object sender, KeyPressEventArgs e)

{

    if (e.KeyChar == '1')

    {

        btn1_Click(null, null);

    }

    else if (e.KeyChar == '2')

    {

        btn2_Click(null, null);

    }

    //Go write your code with else if.........


}

然后,當您想在窗體中的任何位置捕獲 KeyPress 事件時,下面的代碼會將上述 KeyPress 事件添加到窗體上的所有控件,以便您可以在窗體中的任何位置捕獲 KeyPress 事件。


private void Form1_Load(object sender, EventArgs e)

{

    foreach (Control ctrl in Controls)

    {

        ctrl.KeyPress += Form1_KeyPress;

    }

}

正如@Rufus L 在他的回答中提到的,您可以只使用 KeyPreview 屬性,該屬性將在實際控件接收到以下代碼之前捕獲其他控件的 KeyPress 事件。


private void Form1_Load(object sender, EventArgs e)

{

    this.KeyPreview = true;

}


查看完整回答
反對 回復 2021-08-29
  • 1 回答
  • 0 關注
  • 169 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號