winform 计算器
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms; namespace 计算器1
{
public partial class Form1 : Form
{
//存储上次点击了什么按钮,0代表什么都没点击,1代表点击了数字按钮,2代表点击了运算符
private int prev = ;
//存储计算的中间结果
private decimal zj = ;
//记录上次按的什么运算符
private string prevysf = "+";
public Form1()
{
InitializeComponent();
}
//数字按钮
private void button15_Click(object sender, EventArgs e)
{
//将事件源转换为按钮
Button btn = sender as Button;
//替换(如果下面文本框内容为0或者上次点击了运算符)
if (prev == || txtbottom.Text == "")
{
txtbottom.Text = btn.Text;
}
//追加(如果下面文本框内容不为0并且上次没有点击运算符)
else
{
txtbottom.Text += btn.Text;
}
//点击了数字按钮
prev = ;
}
//运算符按钮
private void button16_Click(object sender, EventArgs e)
{
Button btn = sender as Button;
//上次按了数字
if (prev == )
{
txttop.Text += txtbottom.Text + btn.Text;
switch (prevysf)
{
case "+":
zj = zj + Convert.ToDecimal(txtbottom.Text);
break;
case "-":
zj = zj - Convert.ToDecimal(txtbottom.Text);
break;
case "*":
zj = zj * Convert.ToDecimal(txtbottom.Text);
break;
case "/":
zj = zj / Convert.ToDecimal(txtbottom.Text);
break;
}
txtbottom.Text = zj.ToString();
}
//上次按了运算符
else
{
string s = txttop.Text;
s = s.Substring(, s.Length - );
s = s + btn.Text;
txttop.Text = s;
}
//点击了运算符
prev = ;
//记录下运算符
prevysf = btn.Text;
}
//清零
private void button1_Click(object sender, EventArgs e)
{
txttop.Text = "";
txtbottom.Text = "";
prev = ;
zj = ;
prevysf = "+";
}
//清零
private void button2_Click(object sender, EventArgs e)
{
txtbottom.Text = "";
}
//等号
private void button20_Click(object sender, EventArgs e)
{
Button btn = sender as Button;
txttop.Text += txtbottom.Text + btn.Text; switch (prevysf)
{
case "+":
zj = zj + Convert.ToDecimal(txtbottom.Text);
break;
case "-":
zj = zj - Convert.ToDecimal(txtbottom.Text);
break;
case "*":
zj = zj * Convert.ToDecimal(txtbottom.Text);
break;
case "/":
zj = zj / Convert.ToDecimal(txtbottom.Text);
break;
} txtbottom.Text = zj.ToString();
txttop.Text = ""; zj = ;
}
//小数点
private void button19_Click(object sender, EventArgs e)
{
if(txtbottom.Text.Contains(".")==false)
txtbottom.Text += ".";
}
//退格
private void button3_Click(object sender, EventArgs e)
{
txtbottom.Text = txtbottom.Text.Substring(,txtbottom.TextLength-);
}
//±号控制
private void button17_Click(object sender, EventArgs e)
{
if(!txtbottom.Text.Contains("-"))
txtbottom.Text= txtbottom.Text.Insert(, "-");
else if (txtbottom.Text.Contains("-"))
txtbottom.Text = txtbottom.Text.Replace("-","");
}
//键盘控制
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
// if it is a hotkey, return true; otherwise, return false
switch (keyData)
{
case Keys.NumPad0:
//焦点定位到控件button_num_0上,即数字0键上
button18.Focus();
//执行按钮点击操作
button18.PerformClick();
return true;
case Keys.NumPad1:
button13.Focus();
button13.PerformClick();
return true;
case Keys.NumPad2:
button14.Focus();
button14.PerformClick();
return true;
case Keys.NumPad3:
button15.Focus();
button15.PerformClick();
return true;
case Keys.NumPad4:
button9.Focus();
button9.PerformClick();
return true;
case Keys.NumPad5:
button10.Focus();
button10.PerformClick();
return true;
case Keys.NumPad6:
button11.Focus();
button11.PerformClick();
return true;
case Keys.NumPad7:
button5.Focus();
button5.PerformClick();
return true;
case Keys.NumPad8:
button6.Focus();
button6.PerformClick();
return true;
case Keys.NumPad9:
button7.Focus();
button7.PerformClick();
return true;
case Keys.Back:
button3.Focus();
button3.PerformClick();
return true;
case Keys.Divide:
button4.Focus();
button4.PerformClick();
return true;
case Keys.Multiply:
button8.Focus();
button8.PerformClick();
return true;
case Keys.Subtract:
button12.Focus();
button12.PerformClick();
return true;
case Keys.Add:
button16.Focus();
button16.PerformClick();
return true;
case Keys.Enter:
button20.Focus();
button20.PerformClick();
return true;
case Keys.Delete:
button19.Focus();
button19.PerformClick();
return true;
//......
default:
break;
}
return base.ProcessCmdKey(ref msg, keyData);
} }
}
winform 计算器的更多相关文章
- Winform——计算器
namespace 计算器2._0 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } pr ...
- Winform——计算器进制转换
namespace 进制转换2._0 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } p ...
- [原][C#][winForm]分级基金折溢价WinForm网络计算器
分级基金折溢价WinForm网络计算器 通过子/母基金代码,从 [ 东方财富网,天天基金网,新浪 ] 抓取分级基金的子母基金数据(代码,名称,净值,价格), 并计算出子基金(A基金,B基金)以及母基金 ...
- winform操作windows系统计算器
winform对系统计算器的调用,启动,最大化最小化显示,在mainwindow设置topmost=true时,正常显示计算器并置顶. /// <summary> /// 获取窗体的句柄函 ...
- 1.C#WinForm基础制作简单计算器
利用c#语言编写简单计算器: 核心知识点: MessageBox.Show(Convert.ToString(comboBox1.SelectedIndex));//下拉序号 MessageBox.S ...
- winform(四)——简单计算器制作
效果图: 代码区: using System; using System.Collections.Generic; using System.ComponentModel; using System. ...
- winform制作简单计算器
public Form1() { InitializeComponent(); textBox2.Text = ";//主显示屏 textBox1.Text = "";/ ...
- WinForm 制作一个简单的计算器
namespace WindowsFormsApplication6 { public partial class Form1 : Form { //存储上次点击了什么按钮,0代表什么都没有点击,1代 ...
- C# winform(计算器)
随机推荐
- 【bzoj2333】 [SCOI2011]棘手的操作 可并堆+lazy标记
2016-05-31 21:45:41 题目:http://www.lydsy.com/JudgeOnline/problem.php?id=2333 (学习了黄学长的代码 有如下操作: U x y ...
- 如何在osg中删除EventHandler
最近在一个项目中需要动态的添加和删除EventHandler,添加的时候很顺利,使用view->addEventHandler()函数就可以了. 不过在删除的时候,出现点麻烦. 直接调用vi ...
- mapreduce作业状态一直是ACCEPTED
搭建yarn环境后,执行 hadoop/bin/hadoop jar hadoop/share/hadoop/mapreduce/hadoop-mapreduce-examples-2.4.1.jar ...
- 转载:C# this.invoke()作用 多线程操作UI 理解二
Invoke()的作用是:在应用程序的主线程上执行指定的委托.一般应用:在辅助线程中修改UI线程( 主线程 )中对象的属性时,调用this.Invoke(); //测试的窗体 public class ...
- 使用JDK自带的visualvm进行性能监测和调优
使用JDK自带的visualvm进行性能监测和调优 1.关于VisualVm工具 VisualVM 提供在 Java 虚拟机 (Java Virutal Machine, JVM) 上运行的 J ...
- android-数据存储之SharedPreferences
数据存储:SharedPreferences 一.基础概要 1.说明 1>主要用于存储单一小数据: 2>存储类型:boolean.float.String.long.int 3>数据 ...
- thinkphp 的save()不能更新数据解决办法
用save()方法始终更新不了数据,又不显示明确的错误信息,找了好久才在手册里看到一句至关重要的话: 为了保证数据库的安全,避免出错更新整个数据表,如果没有任何更新条件,数据对象本身也不包含主键字段的 ...
- ArcGIS AddIN开发之自定义鼠标样式
如果想修改Windows默认的鼠标样式,可以这样 //设置鼠标样式 this.Cursor = System.Windows.Forms.Cursors.Cross; 可是如果想设置成一些自定义的很好 ...
- hdu N皇后问题
此题是很基本的dfs的题目 ,但是要打表,否则会超时. 这题的思路就是从第一行一直放到第n行,因此行方面的判断就可以省略了.因此只要判断列方面和斜线方面是否满足条件,列方面用一个vis数组来记录是否已 ...
- C# 常用数据库连接字符串【转】
一:C# 连接SQL数据库 Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myP ...