想要将键盘输入添加到我的计算器.同时隐藏鼠标光标
本文关键字:计算器 隐藏 光标 鼠标 我的 键盘 输入 添加 | 更新日期: 2023-09-27 18:33:14
我是c#的新手。我做了一个简单的计算器。但它没有键盘输入。如何启用它?
-
我想在从键盘按下数字 5 s 时在文本框中得到 5
。 我还想在文本字段中隐藏鼠标光标。现在,当应用程序星标时,鼠标光标会出现在文本字段中。
这是我的代码:
namespace MyCalculator
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
//textBox1.Text = "0";
}
double num1=0, num2, result;
string op;
private void button_plus_Click(object sender, EventArgs e)
{
if (textBox1.Text != "")
num1 = Convert.ToDouble(textBox1.Text);
textBox1.Text = String.Empty;
op = "+";
}
private void button_minus_Click(object sender, EventArgs e)
{
if (textBox1.Text != "")
num1 = Convert.ToDouble(textBox1.Text);
textBox1.Text = String.Empty;
op = "-";
}
private void button_mul_Click(object sender, EventArgs e)
{
if (textBox1.Text != "")
num1 = Convert.ToDouble(textBox1.Text);
textBox1.Text = String.Empty;
op = "*";
}
private void button_div_Click(object sender, EventArgs e)
{
if (textBox1.Text != "")
num1 = Convert.ToDouble(textBox1.Text);
textBox1.Text = String.Empty;
op = "/";
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "1";
}
private void button2_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "2";
}
private void button3_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "3";
}
private void button4_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "4";
}
private void button5_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "5";
}
private void button6_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "6";
}
private void button7_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "7";
}
private void button8_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "8";
}
private void button9_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "9";
}
private void button0_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "0";
}
private void button00_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + "00";
}
private void button_point_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + ".";
}
private void button_clear_Click(object sender, EventArgs e)
{
textBox1.Text = String.Empty;
}
private void button_result_Click(object sender, EventArgs e)
{
calculate(op);
}
public void calculate( string op)
{
num2 = Convert.ToDouble(textBox1.Text);
switch(op)
{
case "+" : result=num1+num2;
textBox1.Text = result.ToString(); break;
case "-": result = num1 - num2;
textBox1.Text = result.ToString(); break;
case "*": result = num1 * num2;
textBox1.Text = result.ToString(); break;
case "/": result = num1 / num2;
textBox1.Text = result.ToString(); break;
}
num1 = 0; num2 = 0;
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
textBox1.Font = new Font("Arial",12, FontStyle.Bold);
textBox1.Cursor = Cursors.Arrow;
}
}
}
要捕获键盘KeyDown
事件,您必须首先启用该表单KeyPreview
。选择窗体并转到"属性"并设置KeyPreview = true
。
使用KeyDown
事件捕获键盘事件,
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode.Equals(Keys.NumPad5))
{
//Assuming button5 will set the value 5
button5.PerformClick();
}
}
Hiding Cursor
我假设您期望与Windows计算器相同的行为。您可以禁用文本框,而不是简单地隐藏光标。将文本框设置为 Enabled = false
。