请问开始时为什么无法在窗体上操作,必须先用键盘输入后,才能用窗体的按钮
using System;using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace 计算器的设计
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
double dblnumA = 0;
double dblnumB = 0;
//定义操作符
static string oper = "";
//定义操作数是否有小数点
static bool point = false;
//记录小数点后数字的个数
int dotnum = 0;
//用于保存最初的操作数,以便进行连续敲击等号运算
double dblnum = 0;
//用于记录是否连续敲击等号按钮
bool takeequal = true;
private void display(int i)
{
if (point)
{
dotnum++;
if (i == 0)
{
this.textBox1.Text = this.textBox1.Text + "0";
}
if (i != 0)
{
this.textBox1.Text = Convert.ToString(Convert.ToDouble(this.textBox1.Text) + i / (Math.Pow(10, dotnum)));
}
}
else
{
this.textBox1.Text = this.textBox1.Text.Substring(0, this.textBox1.Text.Length - 1);
this.textBox1.Text = Convert.ToString(Convert.ToDouble(this.textBox1.Text) * 10 + i);
this.textBox1.Text = this.textBox1.Text + ".";
}
}
private void button9_Click(object sender, EventArgs e)
{
display(0);
}
private void button8_Click(object sender, EventArgs e)
{
display(1);
}
private void button11_Click(object sender, EventArgs e)
{
display(2);
}
private void button14_Click(object sender, EventArgs e)
{
display(3);
}
private void button7_Click(object sender, EventArgs e)
{
display(4);
}
private void button10_Click(object sender, EventArgs e)
{
display(5);
}
private void button13_Click(object sender, EventArgs e)
{
display(6);
}
private void button3_Click(object sender, EventArgs e)
{
display(7);
}
private void button4_Click(object sender, EventArgs e)
{
display(8);
}
private void button5_Click(object sender, EventArgs e)
{
display(9);
}
private void operate(string sign)
{
dblnumA = double.Parse(this.textBox1.Text);
point = false;
this.textBox1.Text = "0.";
oper = sign;
takeequal = true;
}
private void button19_Click(object sender, EventArgs e)
{
operate("/");
}
private void button20_Click(object sender, EventArgs e)
{
operate("*");
}
private void button21_Click(object sender, EventArgs e)
{
operate("-");
}
private void button22_Click(object sender, EventArgs e)
{
operate("+");
}
private void button16_Click(object sender, EventArgs e)
{
operate("%");
}
private void button12_Click(object sender, EventArgs e)
{
this.textBox1.Text = Convert.ToString(double.Parse(this.textBox1.Text) *(-1));
}
private void button6_Click(object sender, EventArgs e)
{
if (double.Parse(this.textBox1.Text) != Convert.ToInt64(double.Parse(this.textBox1.Text))||point)
{
this.textBox1.Text = this.textBox1.Text.Substring(0, this.textBox1.Text.Length);
point = false;
}
else
{
this.textBox1.Text = this.textBox1.Text + "";
this.textBox1.Text = this.textBox1.Text.Substring(0, this.textBox1.Text.Length - 1);
}
this.textBox1.Text = Convert.ToString(Math.Sqrt(Double.Parse(this.textBox1.Text)));
takeequal = false;
}
private void button18_Click(object sender, EventArgs e)
{
if (takeequal)
{
dblnum = dblnumB = double.Parse(this.textBox1.Text);
if (oper == "+")
{
this.textBox1.Text = Convert.ToString(dblnumA + dblnumB);
}
if (oper == "-")
{
this.textBox1.Text = Convert.ToString(dblnumA - dblnumB);
}
if (oper == "*")
{
this.textBox1.Text = Convert.ToString(dblnumA * dblnumB);
}
if (oper == "/")
{
if (dblnumB == 0)
{
MessageBox.Show("除数为零,非法操作!");
}
else
this.textBox1.Text = Convert.ToString(dblnumA / dblnumB);
}
if (oper == "%")
{
this.textBox1.Text = Convert.ToString(dblnumB % dblnumB);
}
else
if (double.Parse(this.textBox1.Text) == Convert.ToInt64(double.Parse(this.textBox1.Text))
&& 0 != double.Parse(this.textBox1.Text))
{
this.textBox1.Text = this.textBox1.Text + ".";
}
}
else
WorkLikeFront();
takeequal = false;
}
//退格键
private void button1_Click(object sender, EventArgs e)
{
this.textBox1.Text = this.textBox1.Text.Substring(0, this.textBox1.Text.Length - 1);
if (this.textBox1.Text == "")
{
this.textBox1.Text = "0.";
}
}
//清除键
private void button2_Click(object sender, EventArgs e)
{
this.textBox1.Text = "0";
oper = "";
point = false;
dotnum = 0;
dblnumA = 0;
takeequal = true;
}
private void button17_Click(object sender, EventArgs e)
{
this.textBox1.Text = this.textBox1.Text.Substring(0, this.textBox1.Text.Length);
this.textBox1.Text = Convert.ToString(1 / double.Parse(this.textBox1.Text));
}
private void button15_Click(object sender, EventArgs e)
{
this.textBox1.Text = this.textBox1.Text.Substring(0, this.textBox1.Text.Length - 1);
this.textBox1.Text = this.textBox1.Text = ".";
point = true;
}
private void WorkLikeFront()
{
double dblNumB2 = double.Parse(this.textBox1.Text);
if (oper == "+")
{
this.textBox1.Text = Convert.ToString(dblNumB2 + dblnum);
}
if (oper == "-")
{
this.textBox1.Text = Convert.ToString(dblNumB2 + dblnum);
}
if (oper == "*")
{
this.textBox1.Text = Convert.ToString(dblNumB2 * dblnum);
}
if (oper == "/")
{
if (dblnum == 0)
{
MessageBox.Show("除数为零,非法操作!");
}
else
this.textBox1.Text = Convert.ToString(dblNumB2 / dblnum);
}
if (oper == "%")
{
this.textBox1.Text = Convert.ToString(dblNumB2 % dblnum);
}
else
if (double.Parse(this.textBox1.Text) == Convert.ToInt64(double.Parse(this.textBox1.Text))
&& 0 != double.Parse(this.textBox1.Text))
{
this.textBox1.Text = this.textBox1.Text + ".";
}
}
}
}
这是一个计算器