要求用 c#中的事件 编写
解决方案 »
- Datatable中已经有列,往里面填充数据时不想再增加列
- 设计一个考勤系统,求思路,本人刚入门。。
- 帮我写一些C#正则,来操作自定义的标签,谢谢!!!
- 工行银企互联
- 关于VS.net2005中的DataGridView控件的问题
- 如何在客户端验证IP地址的格式,急
- 请问高手们:我要把GRID中修改的数据保存到SQLSERVER服务器上去,应该怎么做呀?
- cookie,cookies,cookiecollection,cookiecontainer,cookie文件名之间的关系?
- 100分求解:求高效的让关键字变色的方法
- 毕设求助
- 关于SendMessage向listview发送消息,改变图标排列方式的问题。
- C#中webbrowser怎样模拟按下某一个图片或者按钮?
step 1
建一个窗口应用程序项目
step 2
对窗口进行界面设计,要求将界设计成一个计算器的样子
step 3
直接通过界面上的按钮的Click事件(界面双击就可以自动转到代码)来部署业务逻辑
step 4
将各个Click事件中的计算结果,显示到"计算器屏幕"上(可以是一个TextBox)为此
在类级别定义几个关于计算的字段
界面设计部分:
也就是界面的布局了。要做的想windows 中的那样。
实现部分了:
比如说按键的部分:按下什么了,就是button事件了。这个好写。
数据结构部分:
就是,采用栈的数据结构了。
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace WindowsApplication4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
txtHidden1.Text = "";
txtHidden1.Visible = false;
txtHidden2.Text = "";
txtHidden2.Visible = false;//txtHidden1和txtHidden2的功能都是临时存放操作符
txtTemp.Text = "";//txtTemp的功能是存放临时结果
txtTemp.Visible = false;
txtResult.Text = "0";
txtResult.ReadOnly = true;//txtResult的功能是结果的显示以及数字的输入
} private void btn0_Click(object sender, EventArgs e)//按钮0
{
PressNumBtn(sender);
} private void btn1_Click(object sender, EventArgs e)//按钮1
{
PressNumBtn(sender);
} private void btn2_Click(object sender, EventArgs e)//按钮2
{
PressNumBtn(sender);
} private void btn3_Click(object sender, EventArgs e)//按钮3
{
PressNumBtn(sender);
} private void btn4_Click(object sender, EventArgs e)//按钮4
{
PressNumBtn(sender);
} private void btn5_Click(object sender, EventArgs e)//按钮5
{
PressNumBtn(sender);
} private void btn6_Click(object sender, EventArgs e)//按钮6
{
PressNumBtn(sender);
} private void btn7_Click(object sender, EventArgs e)//按钮7
{
PressNumBtn(sender);
} private void btn8_Click(object sender, EventArgs e)//按钮8
{
PressNumBtn(sender);
} private void btn9_Click(object sender, EventArgs e)//按钮9
{
PressNumBtn(sender);
} private void btnDian_Click(object sender, EventArgs e)//按钮小数点
{
if (txtResult.Text == "")
{
return;
}
else if (txtResult.Text.Contains("."))
{
return;
}
else
{
txtResult.Text = txtResult.Text + ".";
}
} private void BtnJia_Click(object sender, EventArgs e)//按钮+
{
PressOperBtn(sender);
} private void btnJian_Click(object sender, EventArgs e)//按钮-
{
PressOperBtn(sender);
} private void btnCheng_Click(object sender, EventArgs e)//按钮*
{
PressOperBtn(sender);
} private void btnChu_Click(object sender, EventArgs e)//按钮除
{
PressOperBtn(sender);
} private void btnEqual_Click(object sender, EventArgs e)//按钮=
{
if (txtHidden2.Text != "" && txtTemp.Text != "")
{
txtResult.Text = WorkOut();
}
txtHidden1.Text = "";
txtHidden2.Text = "";
txtTemp.Text = "";
} private void btnCE_Click(object sender, EventArgs e)//按钮清0
{
txtResult.Text = "0";
txtHidden1.Text = "";
txtHidden2.Text = "";
txtTemp.Text = "";
} private void PressNumBtn(object sender)//操作数字键从0到9
{
Button btn = sender as Button;
string tag = btn.Text;
if (txtHidden1.Text == "")
{
if (txtResult.Text == "0")
{
txtResult.Text = tag;
}
else
{
txtResult.Text = txtResult.Text + tag;
}
}
else
{
txtHidden2.Text = txtHidden1.Text;
txtHidden1.Text = "";
txtResult.Text = tag;
}
} private void PressOperBtn(object sender)//操作加、减、乘、除
{
Button btn = sender as Button;
string tag = btn.Text;
if (txtHidden2.Text != "" && txtTemp.Text != "")
{
txtResult.Text = WorkOut();
}
txtHidden1.Text = tag;
txtTemp.Text = txtResult.Text;
} private string WorkOut()//核心功能:计算
{
string oper = txtHidden2.Text;
double num1 = Convert.ToDouble(txtTemp.Text);
double num2 = Convert.ToDouble(txtResult.Text);
double result=0;
switch (oper)
{
case "+": result = num1 + num2; break;
case "-": result = num1 - num2; break;
case "*": result = num1 * num2; break;
case "/": result = num1 / num2; break;
default: break;
}
string Result = result.ToString();
return Result;
}
}
}
建议你最好弄明白事件如何用了在说。其实明白后,很easy的。
*****************************************************************************
欢迎使用CSDN论坛专用阅读器 : CSDN Reader(附全部源代码) http://feiyun0112.cnblogs.com/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace WindowsForm
{
public partial class Calnder : Form
{
public double a;
public Calnder()
{
InitializeComponent();
}
//获取按钮上的值并且处理第一个数字为0的情况。
private void button1_Click(object sender, EventArgs e)
{
if (this.textBox1.Text.IndexOf(".") != -1)
{
this.textBox1.Text = this.textBox1.Text + this.button1.Text;
}
else
{
this.textBox1.Text = Convert.ToString(Convert.ToDouble(this.textBox1.Text)*10+1);
}
}
//加载时候初始化系统值为0
private void Calnder_Load(object sender, EventArgs e)
{
this.textBox1.Text = "0";
} private void button2_Click(object sender, EventArgs e)
{
if (this.textBox1.Text.IndexOf(".") != -1)
{
this.textBox1.Text = this.textBox1.Text + this.button2.Text;
}
else
{
this.textBox1.Text = Convert.ToString(Convert.ToDouble(this.textBox1.Text) * 10 + 2);
}
} private void button3_Click(object sender, EventArgs e)
{
if (this.textBox1.Text.IndexOf(".") != -1)
{
this.textBox1.Text = this.textBox1.Text + this.button3.Text;
}
else
{
this.textBox1.Text = Convert.ToString(Convert.ToDouble(this.textBox1.Text) * 10 + 3);
}
} private void button4_Click(object sender, EventArgs e)
{
if (this.textBox1.Text.IndexOf(".") != -1)
{
this.textBox1.Text = this.textBox1.Text + this.button4.Text;
}
else
{
this.textBox1.Text = Convert.ToString(Convert.ToDouble(this.textBox1.Text) * 10 + 4);
}
} private void button5_Click(object sender, EventArgs e)
{
if (this.textBox1.Text.IndexOf(".") != -1)
{
this.textBox1.Text = this.textBox1.Text + this.button5.Text;
}
else
{
this.textBox1.Text = Convert.ToString(Convert.ToDouble(this.textBox1.Text) * 10 + 5);
}
} private void button6_Click(object sender, EventArgs e)
{
if (this.textBox1.Text.IndexOf(".") != -1)
{
this.textBox1.Text = this.textBox1.Text + this.button6.Text;
}
else
{
this.textBox1.Text = Convert.ToString(Convert.ToDouble(this.textBox1.Text) * 10 + 6);
}
} private void button7_Click(object sender, EventArgs e)
{
if (this.textBox1.Text.IndexOf(".") != -1)
{
this.textBox1.Text = this.textBox1.Text + this.button7.Text;
}
else
{
this.textBox1.Text = Convert.ToString(Convert.ToDouble(this.textBox1.Text) * 10 + 7);
}
} private void button8_Click(object sender, EventArgs e)
{
if (this.textBox1.Text.IndexOf(".") != -1)
{
this.textBox1.Text = this.textBox1.Text + this.button8.Text;
}
else
{
this.textBox1.Text = Convert.ToString(Convert.ToDouble(this.textBox1.Text) * 10 + 8);
}
} private void button9_Click(object sender, EventArgs e)
{
if (this.textBox1.Text.IndexOf(".") != -1)
{
this.textBox1.Text = this.textBox1.Text + this.button9.Text;
}
else
{
this.textBox1.Text = Convert.ToString(Convert.ToDouble(this.textBox1.Text) * 10 + 9);
}
} private void button11_Click(object sender, EventArgs e)
{
if (this.textBox1.Text.IndexOf(".") != -1)
{
this.textBox1.Text = this.textBox1.Text + this.button10.Text;
}
else
{
this.textBox1.Text = Convert.ToString(Convert.ToDouble(this.textBox1.Text) * 10 + 0);
} } private void button13_Click(object sender, EventArgs e)
{
this.textBox1.Text = "0";
this.textBox2.Text = "0";
}
//小数点情况处理,只能有一个小数点
private void button10_Click(object sender, EventArgs e)
{
if (this.textBox1.Text.IndexOf(".") != -1)
{
System.Windows.Forms.MessageBox.Show(this, "操作错误");
}
else {
this.textBox1.Text = this.textBox1.Text + ".";
}
}
//退出
private void button14_Click(object sender, EventArgs e)
{
this.Close();
}
//加
private void button15_Click(object sender, EventArgs e)
{
a = Convert.ToDouble(this.textBox1.Text);
this.textBox1.Text = "0";
this.textBox2.Clear();
this.textBox2.Text = this.button15.Text;
}
//减
private void button16_Click(object sender, EventArgs e)
{
a = Convert.ToDouble(this.textBox1.Text);
this.textBox1.Text = "0";
this.textBox2.Clear();
this.textBox2.Text = this.button16.Text;
}
//乘
private void button17_Click(object sender, EventArgs e)
{
a = Convert.ToDouble(this.textBox1.Text);
this.textBox1.Text = "0";
this.textBox2.Clear();
this.textBox2.Text = this.button17.Text;
}
//除
private void button18_Click(object sender, EventArgs e)
{
a =Convert.ToDouble( this.textBox1.Text);
this.textBox1.Text = "0";
this.textBox2.Clear();
this.textBox2.Text = this.button18.Text;
} private void button12_Click(object sender, EventArgs e)
{
double b, c;
if (this.textBox2.Text.Equals("+")){
b = Convert.ToDouble(this.textBox1.Text);
c = a + b;
this.textBox1.Text = Convert.ToString(c);
}
if (this.textBox2.Text.Equals("-"))
{
b = Convert.ToDouble(this.textBox1.Text);
c = a - b;
this.textBox1.Text = Convert.ToString(c);
}
if (this.textBox2.Text.Equals("*"))
{
b = Convert.ToDouble(this.textBox1.Text);
c = a * b;
this.textBox1.Text = Convert.ToString(c);
}
if (this.textBox2.Text.Equals("/"))
{
b = Convert.ToDouble(this.textBox1.Text);
c = a / b;
this.textBox1.Text = Convert.ToString(c);
}
} private void textBox2_TextChanged(object sender, EventArgs e)
{ }
}
}由于本人刚学没有一周,有什么不理想的地方请高手指教
去下载吧!
txt1.Text = old.Substring(0, old.Length - 1);//删掉最后一个字符