【问题标题】:Linked keyboard keys are behaving differently than the form buttons they're linked to链接的键盘键与它们链接的表单按钮的行为不同
【发布时间】:2021-02-27 01:43:07
【问题描述】:

我正在编写一个计算器应用程序,我想将按钮与其关联的键盘键链接起来。当我单击键盘键时,它会填充文本框,但不会像单击表单按钮时那样保存值,并且操作键在不应该显示在文本框中时。

这是我的代码:

private void Form1_KeyUp(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.D0:
                    btn0.PerformClick();
                    break;

                case Keys.D1:
                    btn1.PerformClick();
                    break;

                case Keys.D2:
                    btn2.PerformClick();
                    break;

                case Keys.D3:
                    btn3.PerformClick();
                    break;

                case Keys.D4:
                    btn4.PerformClick();
                    break;

                case Keys.D5:
                    btn5.PerformClick();
                    break;

                case Keys.D6:
                    btn6.PerformClick();
                    break;

                case Keys.D7:
                    btn7.PerformClick();
                    break;

                case Keys.D8:
                    btn8.PerformClick();
                    break;

                case Keys.D9:
                    btn9.PerformClick();
                    break;

                case Keys.Add:
                    btnAdd.PerformClick();
                    break;

                case Keys.Subtract:
                    btnSubtract.PerformClick();
                    break;

                case Keys.Multiply:
                    btnMultiply.PerformClick();
                    break;

                case Keys.Divide:
                    btnDivide.PerformClick();
                    break;

                case Keys.Decimal:
                    btnDecimalPoint.PerformClick();
                    break;

                case Keys.Back:
                    btnBack.PerformClick();
                    break;

                default:
                    break;
            }
        }
        private void NumberButton_Click(object sender, EventArgs e)
        {
            if (txtBoxString == "0")  //Gets rid of unwanted leading zeroes
            {
                txtDisplayCalculations.Text = "";
                txtBoxString = "";
            }

            string numberButtonClicked; //String will hold most recent digit

            string senderString = sender.ToString(); //The the string associated with sender
            string[] senderNumber = senderString.Split(); //Split the string 
            numberButtonClicked = senderNumber.Last(); //Get the button's text from the string

            txtBoxString += numberButtonClicked; //Add the most recent digit to the number string
            txtDisplayCalculations.Text = txtBoxString; //Display string in textbox


        }

【问题讨论】:

  • 我猜你应该将文本框设置为只读。
  • 请提供有关“操作员键在不应该显示在文本框中的内容”的更多详细信息。

标签: c# .net visual-studio user-interface keyboard


【解决方案1】:

正如proximab所说,您可以将文本框的ReadOnly 属性设置为true

textBox.ReadOnly = true;

这样,文本框只能通过点击按钮获取值。

但没有像单击表单按钮时那样保存值

是不是表示输入的值没有被记录?尝试在Form1_KeyUp中添加断点,检查事件是否触发。

如果不是,请将表单的KeyPreview 属性设置为true

this.KeyPreview = true;

操作键不应该显示在文本框中

由于没有操作员相关按钮的代码,我们无法重现您描述的问题。请提供Minimal, Reproducible Example,如1+1

【讨论】:

    猜你喜欢
    • 2017-06-09
    • 2021-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-13
    • 2013-08-11
    相关资源
    最近更新 更多