2017-03-25 12 views
0

私はビジュアルスタジオ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

単純な解決策として、ボタンの「タグ」プロパティにテキストボックスに実際に追加する値を置き、button.Textの代わりにbutton.ToString()を追加します。より良い解決策は、各ボタンを別々に処理し、別の方法にコールスルーすることです。例えば1をクリックすると、作成したメソッドが呼び出されます:Insert( "1")など – john

答えて

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; 
     } 
    } 

}

Basiclyあなたはここに 'R' と入力するフォームが赤にその背景色を変更します。 'b'と入力すると青に変わり、 'g'と入力すると緑に変わります。

これが機能するには、コンストラクタでKeyPreviewをtrueに設定する必要があります。

ここでは、コントロールまたはフォームから派生したときにイベントにロジックを追加するための方法として、OnKeyPressイベントをオーバーライドしています。ただし、OnKeyPressメソッドと同じコードブロックを使用する場合は、KeyPressイベントハンドラをフォームに添付することができます。

また、テキストのプロパティから '&'を復元します。

・ホープ、このヘルプ ダニー

+0

ありがとう、サンプルを送っていただけますか? 私は非常に新しいプログラマーです。あなたが私にもう少し説明したら、この「keypress」イベントをどこに追加すればいいでしょうか? ありがとう –

+0

私に数分を与えると私は私が思い付くことができるかを見ていきます。 – dannyhut

+0

私は上記の例を加えて私の答えを編集しました。 – dannyhut

関連する問題