所以我有一个用C#编写的工作计算器(下面包括完整代码),我只能使用计算器按钮输入数字和符号。我希望能够使用键盘和按钮作为输入设备。我的问题是我不知道怎么回事,因为我只学了一个星期的c#。我希望你们中的一位比我更有经验和更聪明的人来编写代码:)包括完整的计算器程序,并感谢您提前。
using System;
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 WindowsFormsApplication2
{
public partial class Form1 : Form
{
string input = string.Empty;
string operand1 = string.Empty;
string operand2 = string.Empty;
char operation;
double result = 0.0;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
input += "1";
this.textBox1.Text += input;
}
private void button2_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
input += "2";
this.textBox1.Text += input;
}
private void button3_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
input += "3";
this.textBox1.Text += input;
}
private void button4_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
input += "4";
this.textBox1.Text += input;
}
private void button5_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
input += "5";
this.textBox1.Text += input;
}
private void button6_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
input += "6";
this.textBox1.Text += input;
}
private void button7_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
input += "7";
this.textBox1.Text += input;
}
private void button8_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
input += "8";
this.textBox1.Text += input;
}
private void button9_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
input += "9";
this.textBox1.Text += input;
}
private void button10_Click(object sender, EventArgs e)
{
operand2 = input;
double num1, num2;
double.TryParse(operand1, out num1);
double.TryParse(operand2, out num2);
this.textBox1.Text = "";
this.input = string.Empty;
this.operand1 = string.Empty;
this.operand2 = string.Empty;
if (operation == '+')
{
result = num1 + num2;
textBox1.Text = result.ToString();
}
else if (operation == '-')
{
result = num1 - num2;
textBox1.Text = result.ToString();
}
else if (operation == '*')
{
result = num1 * num2;
textBox1.Text = result.ToString();
}
else if (operation == '/')
{
if (num2 != 0)
{
result = num1 / num2;
textBox1.Text = result.ToString();
}
else
{
textBox1.Text = "undefined";
}
}
}
private void button11_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
this.input = string.Empty;
this.operand1 = string.Empty;
this.operand2 = string.Empty;
}
private void button12_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
input += "0";
this.textBox1.Text += input;
}
private void button13_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
operand1 = input;
operation = '*';
input = string.Empty;
this.textBox1.Text += input;
}
private void button14_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
operand1 = input;
operation = '+';
input = string.Empty;
this.textBox1.Text += input;
}
private void button15_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
operand1 = input;
operation = '-';
input = string.Empty;
this.textBox1.Text += input;
}
private void button16_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
operand1 = input;
operation = '/';
input = string.Empty;
this.textBox1.Text += input;
}
}
}
答案 0 :(得分:0)
查看KeyDown
事件:https://msdn.microsoft.com/en-us/library/system.windows.forms.control.keydown%28v=vs.110%29.aspx
...或KeyPress
事件(取决于您希望如何处理事情):https://msdn.microsoft.com/en-us/library/system.windows.forms.control.keypress(v=vs.110).aspx
...和Keys
枚举:https://msdn.microsoft.com/en-us/library/system.windows.forms.keys%28v=vs.110%29.aspx
您需要适当处理KeyDown
事件(如果您选择,则KeyPress
),如下例所示:
private void Form1_KeyDown(object sender, KeyEventArgs e) {
if (e.KeyCode == Keys.NumPad0){
// do something
}
}
别忘了订阅活动:
this.KeyDown += Form1_KeyDown;
答案 1 :(得分:0)
创建KeyPressEventHandler并分配方法
using System;
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 WindowsFormsApplication1
{
public partial class Form1 : Form
{
string input = string.Empty;
string operand1 = string.Empty;
string operand2 = string.Empty;
char operation;
double result = 0.0;
public Form1()
{
InitializeComponent();
this.KeyPress +=
new KeyPressEventHandler(Form1_KeyPress);
}
void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
//keypressed
System.Console.WriteLine(e.KeyChar);
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
input += "1";
this.textBox1.Text += input;
}
private void button2_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
input += "2";
this.textBox1.Text += input;
}
private void button3_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
input += "3";
this.textBox1.Text += input;
}
private void button4_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
input += "4";
this.textBox1.Text += input;
}
private void button5_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
input += "5";
this.textBox1.Text += input;
}
private void button6_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
input += "6";
this.textBox1.Text += input;
}
private void button7_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
input += "7";
this.textBox1.Text += input;
}
private void button8_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
input += "8";
this.textBox1.Text += input;
}
private void button9_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
input += "9";
this.textBox1.Text += input;
}
private void button10_Click(object sender, EventArgs e)
{
operand2 = input;
double num1, num2;
double.TryParse(operand1, out num1);
double.TryParse(operand2, out num2);
this.textBox1.Text = "";
this.input = string.Empty;
this.operand1 = string.Empty;
this.operand2 = string.Empty;
if (operation == '+')
{
result = num1 + num2;
textBox1.Text = result.ToString();
}
else if (operation == '-')
{
result = num1 - num2;
textBox1.Text = result.ToString();
}
else if (operation == '*')
{
result = num1 * num2;
textBox1.Text = result.ToString();
}
else if (operation == '/')
{
if (num2 != 0)
{
result = num1 / num2;
textBox1.Text = result.ToString();
}
else
{
textBox1.Text = "undefined";
}
}
}
private void button11_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
this.input = string.Empty;
this.operand1 = string.Empty;
this.operand2 = string.Empty;
}
private void button12_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
input += "0";
this.textBox1.Text += input;
}
private void button13_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
operand1 = input;
operation = '*';
input = string.Empty;
this.textBox1.Text += input;
}
private void button14_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
operand1 = input;
operation = '+';
input = string.Empty;
this.textBox1.Text += input;
}
private void button15_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
operand1 = input;
operation = '-';
input = string.Empty;
this.textBox1.Text += input;
}
private void button16_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
operand1 = input;
operation = '/';
input = string.Empty;
this.textBox1.Text += input;
}
}
}
要获得按下哪个键,请使用
e.KeyChar