using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace 计算器_专业
{
public partial class Frm计算器 : Form
{
public Frm计算器()
{
InitializeComponent();
}
private double currentResult; //临时结果
private string lastOperator;//上一次的操作 private void Frm计算器_Load(object sender, EventArgs e)
{
//遍历数字区的所有控件
foreach (Control cl in this.grp数字区.Controls)
{
if (cl is Button)
{
cl.Click += new EventHandler(数字区cl_Click);
}
}
//为功能区 +- * /四个按钮 ,加上事件
this.btn加.Click += new EventHandler(btn加减乘除_click);
this.btn减.Click += new EventHandler(btn加减乘除_click);
this.btn乘.Click += new EventHandler(btn加减乘除_click);
this.btn除.Click += new EventHandler(btn加减乘除_click);
}
private void 数字区cl_Click(object sender, EventArgs e)
{
//如果显示0.则清空
Button btn数字 = sender as Button;
if (this.txt显示区 .Text =="0"&&btn数字 .Text !=".")
this .txt显示区 .Text =string.Empty ;
//如果是操作符,同样清空
if (this.txt显示区.Text == "+" || this.txt显示区.Text == "*"||this.txt显示区 .Text=="/"||this .txt显示区.Text =="-")
this.txt显示区.Text = string.Empty;
//追加显示
this.txt显示区.Text += btn数字.Text;
} private void btn加减乘除_click(object sender, EventArgs e)
{
//如果上一次操作没有的话(未进行过计算)
//首先调用计算过程
if (!string.IsNullOrEmpty(this.lastOperator))
{
this.btn计算结果等于_Click_1(sender, e);
}
//记录当前显示区内的数字
this.currentResult = Convert.ToDouble(this.txt显示区 .Text );
//事件源
Button btn功能 = sender as Button;
this.lastOperator = btn功能.Text;
this .txt显示区 .Text =this .lastOperator ;
}
private void btn重置_Click(object sender, EventArgs e)
{
this.txt显示区.Text ="0";
this.lastOperator = string.Empty;
this.currentResult = 0;
} private void btn计算结果等于_Click_1(object sender, EventArgs e)
{
//如果当前操作符,不必计算
if (this.txt显示区.Text == "+" || this.txt显示区.Text == "-" || this.txt显示区.Text == "*" || this.txt显示区.Text == "/")
return;
//如果上一次操作符不是空的,则进行计算
if (!string.IsNullOrEmpty(this.lastOperator))
{
//实现功能
switch (this.lastOperator)
{
case "+":
this.currentResult += Convert.ToDouble(this.txt显示区.Text); break;
case "-":
this.currentResult -= Convert.ToDouble(this.txt显示区.Text);
break;
case "*":
this.currentResult *= Convert.ToDouble(this.txt显示区.Text);
break;
case "/":
//非0判断
if (Convert.ToDouble(txt显示区.Text )==0)
{
MessageBox.Show("0不能作为除数!");
return;
}
this.currentResult /= Convert.ToDouble(this.txt显示区.Text);
break; }
//计算完毕,清空上次操作符
this.lastOperator = string.Empty;
//显示结果
this.txt显示区.Text = this.currentResult.ToString();
}
} }
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace 计算器_专业
{
public partial class Frm计算器 : Form
{
public Frm计算器()
{
InitializeComponent();
}
private double currentResult; //临时结果
private string lastOperator;//上一次的操作 private void Frm计算器_Load(object sender, EventArgs e)
{
//遍历数字区的所有控件
foreach (Control cl in this.grp数字区.Controls)
{
if (cl is Button)
{
cl.Click += new EventHandler(数字区cl_Click);
}
}
//为功能区 +- * /四个按钮 ,加上事件
this.btn加.Click += new EventHandler(btn加减乘除_click);
this.btn减.Click += new EventHandler(btn加减乘除_click);
this.btn乘.Click += new EventHandler(btn加减乘除_click);
this.btn除.Click += new EventHandler(btn加减乘除_click);
}
private void 数字区cl_Click(object sender, EventArgs e)
{
//如果显示0.则清空
Button btn数字 = sender as Button;
if (this.txt显示区 .Text =="0"&&btn数字 .Text !=".")
this .txt显示区 .Text =string.Empty ;
//如果是操作符,同样清空
if (this.txt显示区.Text == "+" || this.txt显示区.Text == "*"||this.txt显示区 .Text=="/"||this .txt显示区.Text =="-")
this.txt显示区.Text = string.Empty;
//追加显示
this.txt显示区.Text += btn数字.Text;
} private void btn加减乘除_click(object sender, EventArgs e)
{
//如果上一次操作没有的话(未进行过计算)
//首先调用计算过程
if (!string.IsNullOrEmpty(this.lastOperator))
{
this.btn计算结果等于_Click_1(sender, e);
}
//记录当前显示区内的数字
this.currentResult = Convert.ToDouble(this.txt显示区 .Text );
//事件源
Button btn功能 = sender as Button;
this.lastOperator = btn功能.Text;
this .txt显示区 .Text =this .lastOperator ;
}
private void btn重置_Click(object sender, EventArgs e)
{
this.txt显示区.Text ="0";
this.lastOperator = string.Empty;
this.currentResult = 0;
} private void btn计算结果等于_Click_1(object sender, EventArgs e)
{
//如果当前操作符,不必计算
if (this.txt显示区.Text == "+" || this.txt显示区.Text == "-" || this.txt显示区.Text == "*" || this.txt显示区.Text == "/")
return;
//如果上一次操作符不是空的,则进行计算
if (!string.IsNullOrEmpty(this.lastOperator))
{
//实现功能
switch (this.lastOperator)
{
case "+":
this.currentResult += Convert.ToDouble(this.txt显示区.Text); break;
case "-":
this.currentResult -= Convert.ToDouble(this.txt显示区.Text);
break;
case "*":
this.currentResult *= Convert.ToDouble(this.txt显示区.Text);
break;
case "/":
//非0判断
if (Convert.ToDouble(txt显示区.Text )==0)
{
MessageBox.Show("0不能作为除数!");
return;
}
this.currentResult /= Convert.ToDouble(this.txt显示区.Text);
break; }
//计算完毕,清空上次操作符
this.lastOperator = string.Empty;
//显示结果
this.txt显示区.Text = this.currentResult.ToString();
}
} }
}
解决方案 »
- 想知道Using 的这个用法
- asp.net中url传值显示乱码问题
- TABLE打印 在FIREFOX下面正常 为什么在IE下面就会有200多页呢
- 读取 XML 文件 出现 object not set to an instance, 用了这个方法 SelectSingleNode
- gridview和自定义BUTTON的问题
- 新鸟初学C# 想用SerialPort控件实现Modbus通信协议 读取/写入数据 求大侠指点
- C#中使用DELPHI开发的DLL库,传string值时,只能传11位,请大师们指教!!急!!
- C#窗体应用程序,如何将其他的EXE程序包含在窗体内?
- 新建群:串口通信,GPRRS MODEM短信方面的,欢迎高手加入!
- 对datatable进行查询的问题
- c#如何保存textbox中内容
- 内存中有幅bmp图片 我怎么把它显示在picturebox
没有小数点
else
有小数点
//有小数点
else
//没有小数点
如果没有小数点的话,返回负数,也就是-1
哈哈,这是我个人的看法
但是楼上说的我觉得不完善
要在之前判断计算器的文本框中有没有. 要是有再按时候就提示什么的 要是没有 就允许按下
至于如何判断就用
if(this.txt显示区.Text.IndexOf('.')==-1)
IndexOf就是查字符的位置 要是有该字符 就返回所在字符串中的位置 要是没有就返回-1 明白???
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 46) //小数点的ascii码
{
if (exist_小数点 == false)
{
exist_小数点 = true;
}
else
{
e.Handled = true;
}
}
}
/// <summary>
/// 文本框只能输入数字型和单精度型的字符串.
/// </summary>
/// <param name="e">KeyPressEventArgs类</param>
/// <param name="s">文本框的字符串</param>
/// <param name="n">标识,判断是数字型还是单精度型</param>
public void Estimate_Key(KeyPressEventArgs e,string s,int n)
{
if (n==0) //只能输入整型
if (!(e.KeyChar <= '9' && e.KeyChar >= '0') && e.KeyChar != '\r' && e.KeyChar != '\b')
{
e.Handled = true; //处理KeyPress事件
}
if (n == 1) //可以输入整型或单精度型
{
if ((!(e.KeyChar <= '9' && e.KeyChar >= '0')) && e.KeyChar != '.' && e.KeyChar != '\r' && e.KeyChar != '\b')
{
e.Handled = true;
}
else
{
if (e.KeyChar == '.') //如果输入“.”
if (s == "") //当前文本框为空
e.Handled = true; //处理KeyPress事件
else
{
if (s.Length > 0) //当文本框不为空时
{
if (s.IndexOf(".") > -1) //查找是否已输入过“.”
e.Handled = true; //处理KeyPress事件
}
}
}
}
}
#endregion
{
string[] ss = textBox1.Text.Split(new char[] { '.' });
int ii = ss.Length - 1;
if (ii > 1)
{
textBox1.Select(textBox1.Text.Length - 1, textBox1.Text.Length);
textBox1.Focus();
}
}在textbox 的TextChanged下写事件,用Split 函数把输入的字符转换为以点为分隔符的数组,判断数组长度就可以了。
{
Convert.ToDouble(要输入数字的文本区域)
}
catch(Exception ex)
{
textBox1.Select(textBox1.Text.Length - 1, textBox1.Text.Length);
this.textBox1.Select(this.textBox1.text.length,this.textBox1.text.length+1);
textBox1.Focus();
}
写到它的textchanged事件下
当用户输入一个小数点的时候没有问题当输入两个的时候 它会自动删掉一个`这样也就可以解决只能输入数字不能输入符号或字母了`~ 一举两得
/判断是否存在小数点
bool existdot=false; void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{ if (e.KeyChar == 46&&existdot == false)
{existdot=true;} else if(e.KeyChar==46 && existdot==true)
{ e.Handled = true; }
}