界面设计
思路
1.点击button数字按钮时为textbox进行赋值,点击运算符按钮时进行相应的运算,确保每个按钮在点击时都会有点击事件发生
2.在输入第二个数字时(点击运算符后),将textbox1中的第一个数字进行清除
3.在输出结果时(点击"="后),将textbox1中的第二个数字进行清除,将最终结果显示在textbox1中
程序代码
对窗体的显示位置,textbox中的字体,默认光标位置等进行设置
this.Left = Screen.PrimaryScreen.Bounds.Width / 2 - this.Width / 2; //设置窗体打开后显示在屏幕中央
this.Top = Screen.PrimaryScreen.Bounds.Height / 2 - this.Height / 2;
this.FormBorderStyle = FormBorderStyle.FixedToolWindow; //设置边框格式只有关闭按钮
textBox1.ReadOnly = true; //设置为只读
textBox2.TabIndex = 0; //设置光标默认位置在textbox2中
在设置点击事件前,定义记录空间和判断变量
bool dianjiysf = false; //定义一个变量来判断是否点击了运算符
string type; //用type来记录按下的运算符号
double num1; //记录输入第一个数字
double num2; //记录输入的第二个数字
定义数字按钮的点击事件
数字1
if (dianjiysf==true) //判断是否点击了运算符按钮
{
textBox1.Text = "";
dianjiysf = false;
}
textBox1.Text += 1; //"+" 并不是求和运算 而是用来连接字符串
textBox2.Text += 1;
数字2
if (dianjiysf == true) //判断是否点击了运算符按钮
{
textBox1.Text = "";
dianjiysf = false;
}
textBox1.Text += 2;
textBox2.Text += 2;
数字3
if (dianjiysf == true) //判断是否点击了运算符按钮
{
textBox1.Text = "";
dianjiysf = false;
}
textBox1.Text += 3;
textBox2.Text += 3;
数字4
if (dianjiysf == true) //判断是否点击了运算符按钮
{
textBox1.Text = "";
dianjiysf = false;
}
textBox1.Text += 4;
textBox2.Text += 4;
数字5
if (dianjiysf == true) //判断是否点击了运算符按钮
{
textBox1.Text = "";
dianjiysf = false;
}
textBox1.Text += 5;
textBox2.Text += 5;
数字6
if (dianjiysf == true) //判断是否点击了运算符按钮
{
textBox1.Text = "";
dianjiysf = false;
}
textBox1.Text += 6;
textBox2.Text += 6;
数字7
if (dianjiysf == true) //判断是否点击了运算符按钮
{
textBox1.Text = "";
dianjiysf = false;
}
textBox1.Text += 7;
textBox2.Text += 7;
数字8
if (dianjiysf == true) //判断是否点击了运算符按钮
{
textBox1.Text = "";
dianjiysf = false;
}
textBox1.Text += 8;
textBox2.Text += 8;
数字9
if (dianjiysf == true) //判断是否点击了运算符按钮
{
textBox1.Text = "";
dianjiysf = false;
}
textBox1.Text += 9;
textBox2.Text += 9;
数字0
if (dianjiysf == true) //判断是否点击了运算符按钮
{
textBox1.Text = "";
dianjiysf = false;
}
textBox1.Text += 0;
textBox2.Text += 0;
符号"."
textBox1.Text += ".";
textBox2.Text += ".";
清除键(CE键)点击事件
textBox1.Text = "";
textBox2.Text = "";
运算符按钮的点击事件
" + "
type = "+";
dianjiysf = true;
num1 = double.Parse(textBox1.Text); //转换数据类型 string类型转换为double类型 方法为:double.parse()
textBox2.Text += type; //拼接运算符
" - "
type = "-";
dianjiysf = true;
num1 = double.Parse(textBox1.Text); //转换数据类型 string类型转换为double类型 方法为:double.parse()
textBox2.Text += type; //拼接运算符
" * "
type = "*";
dianjiysf = true;
num1 = double.Parse(textBox1.Text); //转换数据类型 string类型转换为double类型 方法为:double.parse()
textBox2.Text += type; //拼接运算符
" / "
type = "/";
dianjiysf = true;
num1 = double.Parse(textBox1.Text); //转换数据类型 string类型转换为double类型 方法为:double.parse()
textBox2.Text += type; //拼接运算符
" % "
type = "%";
dianjiysf = true;
num1 = double.Parse(textBox1.Text); //转换数据类型 string类型转换为double类型 方法为:double.parse()
textBox2.Text += type; //拼接运算符
等号按钮的点击事件
dianjiysf = true;
double num2 = double.Parse(textBox1.Text); //转换数据类型 string类型转换为double类型 方法为:double.parse()
if (type=="+")
{
textBox1.Text = (num1 + num2).ToString();
}
if (type == "-")
{
textBox1.Text = (num1 - num2).ToString();
}
if (type == "*")
{
textBox1.Text = (num1 * num2).ToString();
}
if (type == "/")
{
textBox1.Text = (num1 / num2).ToString();
}
if (type == "%")
{
textBox1.Text = (num1 % num2).ToString();
}
textBox2.Text += "=" + textBox1.Text;