C#中的计算器使用“&”从键盘输入在文本属性中

时间:2017-03-25 16:24:11

标签: c# input keyboard

我正在视觉工作室2017开发一个计算器。一切正常,但键盘输入不正常。 我用“&”在按钮的文本属性中,它可以工作,但问题是它在屏幕上打印如“& 1 +& 2”。我附上了一个代码和图片,以便你们可以看到发生了什么。

1 - result picture

2 - usage of "&" symbol

提前致谢, 最好的祝福, 拉姆

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Calculator
{
    public partial class Form1 : Form
    {
        Double resultado_value = 0; // result is zero in the beginning 
        String operationPerformed = ""; 
        bool is_pressed = false;
        public Form1()
        {
            InitializeComponent();
        }

        private void button_click(object sender, EventArgs e)
        {
            if ((textBox_Result.Text == "0") || (is_pressed))
                textBox_Result.Clear();

            is_pressed = false;
            Button button = (Button)sender;
            if (button.Text == ".") //to avoid repetitive dots
            { 
               if(!textBox_Result.Text.Contains("."))
                   textBox_Result.Text = textBox_Result.Text + button.Text;

            }else
            textBox_Result.Text = textBox_Result.Text + button.Text;

        }

        private void operator_click(object sender, EventArgs e)
        {
            Button button = (Button)sender;

            if (resultado_value != 0) //if result value not equal to zero
            {
                button15.PerformClick();
                operationPerformed = button.Text;
                labelCurrentOperation.Text = resultado_value + " " + operationPerformed;
                is_pressed = true;
            }
            else
            {
                operationPerformed = button.Text;
                resultado_value = Double.Parse(textBox_Result.Text);
                labelCurrentOperation.Text = resultado_value + " " + operationPerformed;
                is_pressed = true;
            }
        }
        //Clear entry
        private void button4_Click(object sender, EventArgs e)
        {
            textBox_Result.Text = "0";
        }
        //button Clear
        private void button5_Click(object sender, EventArgs e)
        {
           // this.BackColor = System.Drawing.Color.White;//can't find color "control"
            textBox_Result.Text = "0";
            resultado_value = 0;
        }
        // equal button
        private void button15_Click(object sender, EventArgs e)
        {
            switch (operationPerformed)
            {
                case "+":
                  //  this.BackColor = System.Drawing.Color.Red;//form change color to red
                    textBox_Result.Text = (resultado_value + Double.Parse(textBox_Result.Text)).ToString();
                    break;
                case "-":
                //    this.BackColor = System.Drawing.Color.Aqua;
                    textBox_Result.Text = (resultado_value - Double.Parse(textBox_Result.Text)).ToString();
                    break;
                case "X":
                 //   this.BackColor = System.Drawing.Color.AliceBlue;
                    textBox_Result.Text = (resultado_value * Double.Parse(textBox_Result.Text)).ToString();
                    break;
                case "÷":
                  //  this.BackColor = System.Drawing.Color.BlueViolet;
                    textBox_Result.Text = (resultado_value / Double.Parse(textBox_Result.Text)).ToString();
                    break;
                default:
                    break;
            }
            resultado_value = Double.Parse(textBox_Result.Text);
            labelCurrentOperation.Text = "";
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void labelCurrentOperation_Click(object sender, EventArgs e)
        {

        }
    }
}

1 个答案:

答案 0 :(得分:0)

如果我理解你正在尝试做什么,那么你想要做的就是在表单级抓住按键。如果这是您想要的,您应该将表单的KeyPreview属性设置为true并覆盖表单的OnKeyPress方法或添加KeyPressed的事件处理程序,并将其分配给Forms KeyPressed事件并在那里做你的事情。

如果您希望我提供一个示例,请告诉我。

Ram Pawar我已经为你写了一个简单的例子。

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Formkeypress
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            KeyPreview = true;
        }

        protected override void OnKeyPress(KeyPressEventArgs e)
        {
            base.OnKeyPress(e);
            if (e.KeyChar == 'r') BackColor = Color.Red;
            if (e.KeyChar == 'b') BackColor = Color.Blue;
            if (e.KeyChar == 'g') BackColor = Color.Green;
        }
    }

}

基本上,如果您在此处键入“r”,则表单会将其背景颜色更改为红色。输入'b'会将其更改为蓝色,输入'g'会将其更改为绿色。

请注意,您必须在构造函数中将KeyPreview设置为true才能使其生效。

我在这里覆盖OnKeyPress事件,因为这是从控件或表单派生时向事件添加逻辑的首选方法。但是,如果您希望使用与OnKeyPress方法相同的代码块,则可以将KeyPress事件处理程序附加到Form。

还要从文字属性中重新删除'&'。

希望这有帮助 丹尼