>我正在Visual Studio 2017中开发一个计算器。一切正常,但键盘输入无法正常工作。 我在按钮的文本属性中使用"&",它可以工作,但问题是它在屏幕上打印,就像"&1 + &2"一样。我附上代码和图像,以便你们可以看到正在发生的事情。
1 - 结果图片
2 - "&"符号的使用
提前感谢, 此致敬意 公羊
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)
{
}
}
}
如果我了解您要正确执行的操作,那么您要做的是在表单级别捕获按键。如果这是您想要的,则应将窗体的 KeyPreview 属性设置为 true,并重写窗体的 OnKeyPress 方法,或者添加 KeyPress 的事件处理程序并将其分配给窗体 KeyPressed 事件并在那里执行操作。
如果你想让我提供一个例子,请告诉我。
拉姆·帕瓦尔 我为你写了一个快速的例子。
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。
同时从文本属性中重新移动"&"。
希望这个帮助 丹尼