2017-03-25 42 views
0

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

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

     } 
    } 
} 
+0

你怎么_expect_它做什么? &并不代表文本框中的任何内容... – john

+0

@john感谢您的评论!当我将“&”放在文本中时,它开始接受来自键盘的输入。但问题在于它正在结果区域中打印。 –

+0

作为一个简单的解决方案,将要实际添加到文本框中的值添加到按钮的“Tag”属性中,然后添加button.Tag.ToString()而不是button.Text。更好的解决方案是分别处理每个按钮并通过其他方法调用。例如点击1调用你创建的方法:插入(“1”)等等 – john

回答

0

如果我明白你正在尝试做的正确,那么你想要什么do是在表单级别捕获按键。如果这是你想要的,你应该将你的Form的KeyPreview属性设置为true,并重写Form的OnKeyPress方法,或者添加一个KeyPressed的Event Handler,并将它分配给Form KeyPressed Event并在那里做你的事情。

如果你想让我提供一个例子让我知道。

拉姆帕瓦我写了一个快速的例子给你。

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' 在这里Basicly形式将改变其背景颜色为红色。键入'b'会将其更改为蓝色,输入'g'会将其更改为绿色。

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

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

还从您的文本属性中重新移除'&'。

希望这有助于 丹尼

+0

非常感谢你,请你举个例子吗? 我是一个非常新的程序员,所以如果你更多地解释我会更好,我应该在哪里添加这个“按键”事件? 谢谢 –

+0

给我几分钟,我会看看我能想出什么。 – dannyhut

+0

我已经编辑了我的答案,添加了一个例子。 – dannyhut

相关问题