簡體   English   中英

使用文本屬性中的“&”從鍵盤輸入C#中的計算器

[英]Calculator in C# input from keyboard using “&” in text property

我正在Visual Studio 2017中開發計算器。一切工作正常,但是鍵盤輸入無法正常工作。 我在按鈕的text屬性中使用“&”,它可以工作,但是問題是它在屏幕上的顯示方式是“&1 +&2”。 我附上了代碼和圖像,以便您可以看到發生了什么。

1- 結果圖片

2- “&”符號的用法

在此先感謝您,Ram

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)
        {

        }
    }
}

如果我了解您要正確執行的操作,那么您想要執行的操作就是在Form級別捕獲按鍵。 如果這是您想要的,則應將窗體的KeyPreview屬性設置為true並覆蓋窗體的OnKeyPress方法,或添加KeyPressed的事件處理程序,並將其分配給窗體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。

同時從文本屬性中刪除“&”。

希望這對丹尼有幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM