私はビジュアルスタジオ2017で電卓を開発中です。すべてうまくいっていますが、キーボードからの入力が正しく機能していません。 ボタンのテキストプロパティに「&」を使用していますが、問題は「& 1 + & 2」のように画面に印刷されていることです。私はコードと画像を添付して、あなたたちは何が起こっているのかを見ることができます。キーボードのC#での入力でテキストプロパティの "&"を使用して
事前のおかげで、 敬具、 ラム
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)
{
}
}
}
あなたはそれが何をする_expect_ていますか? &テキストボックスに何も意味しない... – john
@johnあなたのコメントをありがとう!テキストに "&"を入れると、キーボードからの入力を受け付け始めます。問題はそれが結果領域に印刷されていることです。 –
単純な解決策として、ボタンの「タグ」プロパティにテキストボックスに実際に追加する値を置き、button.Textの代わりにbutton.ToString()を追加します。より良い解決策は、各ボタンを別々に処理し、別の方法にコールスルーすることです。例えば1をクリックすると、作成したメソッドが呼び出されます:Insert( "1")など – john