winform简易计算器

2021-07-02 19:04

阅读:451

标签:spl   ide   int start   mes   man   object   小数   pen   kernel32   

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp2
{
public partial class Form1 : Form
{
//控制点,1是控制运算符,2是控制小数点
int busy = 1,sub = 0;
string[] sArray = null;
float first_number = 0;
float second_number = 0;
float result = 0;
int judge = 0;
int clear_17 = 0;
public Form1()
{
InitializeComponent();
}

//延时模块
[System.Runtime.InteropServices.DllImport("kernel32.dll")]
static extern uint GetTickCount();
///


/// 程序等待延迟执行
///

///
static void MySleep(uint ms)
{
uint start = GetTickCount();
while (GetTickCount() - start {
Application.DoEvents();
}
}

private void Form1_Load(object sender, EventArgs e)
{

}

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == Keys.Tab) {
richTextBox1.Text += ‘~‘;
}
if ((clear_17 == 1)&&(keyData == Keys.D0 || keyData == Keys.NumPad0 || keyData == Keys.D1 || keyData == Keys.NumPad1 || keyData == Keys.D2 || keyData == Keys.NumPad2 || keyData == Keys.D3 || keyData == Keys.NumPad3 || keyData == Keys.D4 || keyData == Keys.NumPad4 || keyData == Keys.D5 || keyData == Keys.NumPad5 || keyData == Keys.D6 || keyData == Keys.NumPad6 || keyData == Keys.D7 || keyData == Keys.NumPad7 || keyData == Keys.D8 || keyData == Keys.NumPad8 || keyData == Keys.D9 || keyData == Keys.NumPad9))
{
button13.PerformClick();
clear_17 = 0;
}
else if (keyData != Keys.Enter)
{
//clear_17 = 0;
}
switch (keyData)
{
case Keys.D0:
button11.PerformClick();
break;
case Keys.NumPad0:
button11.PerformClick();
break;
case Keys.D1:
button1.PerformClick();
break;
case Keys.NumPad1:
button1.PerformClick();
break;
case Keys.D2:
button2.PerformClick();
break;
case Keys.NumPad2:
button2.PerformClick();
break;
case Keys.D3:
button3.PerformClick();
break;
case Keys.NumPad3:
button3.PerformClick();
break;
case Keys.D4:
button4.PerformClick();
break;
case Keys.NumPad4:
button4.PerformClick();
break;
case Keys.D5:
button5.PerformClick();
break;
case Keys.NumPad5:
button5.PerformClick();
break;
case Keys.D6:
button6.PerformClick();
break;
case Keys.NumPad6:
button6.PerformClick();
break;
case Keys.D7:
button7.PerformClick();
break;
case Keys.NumPad7:
button7.PerformClick();
break;
case Keys.D8:
button8.PerformClick();
break;
case Keys.NumPad8:
button8.PerformClick();
break;
case Keys.D9:
button9.PerformClick();
break;
case Keys.NumPad9:
button9.PerformClick();
break;
case Keys.Decimal:
button12.PerformClick();
break;
case Keys.Delete:
button12.PerformClick();
break;
case Keys.Add:
button10.PerformClick();
break;
case Keys.Subtract:
button14.PerformClick();
break;
case Keys.Multiply:
button15.PerformClick();
break;
case Keys.Divide:
button16.PerformClick();
break;
case Keys.Enter:
button17.PerformClick();
break;
case Keys.Escape:
button13.PerformClick();
break;
case Keys.Back:
button18.PerformClick();
break;
}
return base.ProcessCmdKey(ref msg, keyData);
}

public void Button_judge()
{
if (richTextBox1.Text.ToUpper().Contains(" + ") == true) { judge = 1; }
else if (richTextBox1.Text.ToUpper().Contains(" - " ) == true) { judge = 1; }
else if (richTextBox1.Text.ToUpper().Contains(" * ") == true) { judge = 1; }
else if (richTextBox1.Text.ToUpper().Contains(" ÷ ") == true) { judge = 1; }
}

private void button1_Click(object sender, EventArgs e)
{
if ((busy == 2) || (busy == 3)) { richTextBox1.Text += "1"; busy = 3; }
else { richTextBox1.Text += "1"; busy = 0; sub = 0; }
if (richTextBox1.Text.ToUpper().Contains("~~~321"))
{
//System.Threading.Thread.Sleep(2000);
//Thread.Sleep(1000);
//this.Invoke(new Action(() =>
//{
// richTextBox1.Text = "3 !";//调用处理事件
//}));
//Thread.Sleep(1000);
//this.Invoke(new Action(() =>
//{
// richTextBox1.Text = "2 !";//调用处理事件
//}));
//Thread.Sleep(1000);
//this.Invoke(new Action(() =>
//{
// richTextBox1.Text = "1 !";//调用处理事件
//}));
//Thread.Sleep(1000);
//this.Invoke(new Action(() =>
//{
// richTextBox1.Text = "change beetle !";//调用处理事件
//}));
MySleep(1000);
richTextBox1.Text = "3 !";
MySleep(1000);
richTextBox1.Text = "2 !";
MySleep(1000);
richTextBox1.Text = "1 !";
MySleep(1000);
richTextBox1.Text = "change beetle !";
}
}

private void button2_Click(object sender, EventArgs e)
{
if ((busy == 2) || (busy == 3)) { richTextBox1.Text += "2"; busy = 3; }
else { richTextBox1.Text += "2"; busy = 0; sub = 0; }
}

private void button3_Click(object sender, EventArgs e)
{
if ((busy == 2) || (busy == 3)) { richTextBox1.Text += "3"; busy = 3; }
else { richTextBox1.Text += "3"; busy = 0; sub = 0; }
}

private void button4_Click(object sender, EventArgs e)
{
if ((busy == 2) || (busy == 3)) { richTextBox1.Text += "4"; busy = 3; }
else { richTextBox1.Text += "4"; busy = 0; sub = 0; }
if (richTextBox1.Text.ToUpper().Contains("139~~~8014"))
{
MySleep(2000);
richTextBox1.Text = "He is a Cool Man !";
}
}

private void button5_Click(object sender, EventArgs e)
{
if ((busy == 2) || (busy == 3)) { richTextBox1.Text += "5"; busy = 3; }
else { richTextBox1.Text += "5"; busy = 0; sub = 0; }
if (richTextBox1.Text.ToUpper().Contains("~~~555"))
{
MySleep(1000);
richTextBox1.Text = "STANDING BY.";
MySleep(3000);
richTextBox1.Text = "COMPLETE !";
}
}

private void button6_Click(object sender, EventArgs e)
{
if ((busy == 2) || (busy == 3)) { richTextBox1.Text += "6"; busy = 3; }
else { richTextBox1.Text += "6"; busy = 0; sub = 0; }
}

private void button7_Click(object sender, EventArgs e)
{
if ((busy == 2) || (busy == 3)) { richTextBox1.Text += "7"; busy = 3; }
else { richTextBox1.Text += "7"; busy = 0; sub = 0; }
}

private void button8_Click(object sender, EventArgs e)
{
if ((busy == 2) || (busy == 3)) { richTextBox1.Text += "8"; busy = 3; }
else { richTextBox1.Text += "8"; busy = 0; sub = 0; }
}

private void button9_Click(object sender, EventArgs e)
{
if ((busy == 2) || (busy == 3)) { richTextBox1.Text += "9"; busy = 3; }
else { richTextBox1.Text += "9"; busy = 0; sub = 0; }
}

private void button10_Click(object sender, EventArgs e)
{
Button_judge();
if ((busy == 1) || (busy == 2)) { }
else if (judge == 1) {
this.button17_Click(sender, e);
richTextBox1.Text += " + ";
judge = 0;
}
else { richTextBox1.Text += " + "; busy = 1; }
clear_17 = 0;
}

private void button14_Click(object sender, EventArgs e)
{
Button_judge();
if (richTextBox1.Text.ToCharArray().Length == 0)
{
richTextBox1.Text += "-";
}
//a_sPath.Substring(a_sPath.Length - 1, 1)
else if (richTextBox1.Text.Substring(richTextBox1.Text.ToCharArray().Length - 1, 1) == " ")
{
richTextBox1.Text += "-";
}
else if ((busy == 1) || (busy == 2)) { }
else if (judge == 1)
{
this.button17_Click(sender, e);
richTextBox1.Text += " - ";
judge = 0;
}
else { richTextBox1.Text += " - "; busy = 1; }
clear_17 = 0;
}


//private void button14_Click(object sender, EventArgs e)
//{
// Button_judge();
// if ((sub == 1) || (busy == 2)) { }
// else if (judge == 1)
// {
// try {
// if (richTextBox1.Text.AsQueryable().Last().ToString() == " ")
// {
// richTextBox1.Text = richTextBox1.Text.Replace(" - ", "a");
// sArray = richTextBox1.Text.Split(‘a‘);
// first_number = float.Parse(sArray[0]);
// second_number = float.Parse(sArray[1]);
// if (judge == 1)
// {
// this.button17_Click(sender, e);
// //result = first_number - second_number;
// //richTextBox1.Text = result.ToString();
// busy = 0; sub = 0;
// richTextBox1.Text += " - ";
// judge = 0;
// }
// }
// else
// ;
// }
// catch (Exception){
// int other = 0;
// char[] c = richTextBox1.Text.ToCharArray();
// foreach (char i in c) {
// if (i == ‘-‘) { other++; }
// }
// if (other // //try
// //{
// // string last_word = c.AsQueryable().Last().ToString();
// // string penultimate_word = richTextBox1.Text.LastIndexOf(‘ ‘, richTextBox1.Text.LastIndexOf(" ") - 2).ToString();

// // if (last_word == "a") //&& penultimate_word == "a")
// // {
// // richTextBox1.Text = richTextBox1.Text.Replace("a", " - ");
// // richTextBox1.Text += "-"; busy = 1; sub = 1; judge = 0;
// // }
// // else if (c[0].ToString() == "")
// // {
// // richTextBox1.Text = richTextBox1.Text.Replace("a", " - ");
// // richTextBox1.Text += "-"; busy = 1; sub = 1; judge = 0;
// // }
// //}
// //catch (Exception)
// //{
// // judge = 1;
// //}
// richTextBox1.Text = richTextBox1.Text.Replace("a", " - ");
// richTextBox1.Text += "-"; busy = 1; sub = 1; judge = 0;
// }

// else {
// judge = 1;
// //MessageBox.Show(ex.Message);
// }
// }
// }
// else if (richTextBox1.Text != null && richTextBox1.Text.Length == 0) {
// richTextBox1.Text += "-"; busy = 1;sub = 1;
// }
// else { richTextBox1.Text += " - "; busy = 1;}
// clear_17 = 0;
//}

private void button15_Click(object sender, EventArgs e)
{
Button_judge();
if ((busy == 1) || (busy == 2)) { }
else if (judge == 1)
{
this.button17_Click(sender, e);
richTextBox1.Text += " * ";
judge = 0;
}
else { richTextBox1.Text += " * "; busy = 1; }
clear_17 = 0;
}

private void button16_Click(object sender, EventArgs e)
{
Button_judge();
if ((busy == 1) || (busy == 2)) { }
else if (judge == 1)
{
this.button17_Click(sender, e);
richTextBox1.Text += " ÷ ";
judge = 0;
}
else { richTextBox1.Text += " ÷ "; busy = 1; }
clear_17 = 0;
}

private void button17_Click(object sender, EventArgs e)
{
if ((busy == 1) || (busy == 2) || (clear_17 == 1)) { }
else {
clear_17 = 1;
if (richTextBox1.Text.ToUpper().Contains(" + "))
{
try {
richTextBox1.Text = richTextBox1.Text.Replace(" + ", "a");
sArray = richTextBox1.Text.Split(‘a‘);
first_number = float.Parse(sArray[0]);
second_number = float.Parse(sArray[1]);
result = first_number + second_number;
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
else if (richTextBox1.Text.ToUpper().Contains(" - "))
{
try
{
richTextBox1.Text = richTextBox1.Text.Replace(" - ", "a");
sArray = richTextBox1.Text.Split(‘a‘);
first_number = float.Parse(sArray[0]);
second_number = float.Parse(sArray[1]);
result = first_number - second_number;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
else if (richTextBox1.Text.ToUpper().Contains(" * "))
{
try
{
richTextBox1.Text = richTextBox1.Text.Replace(" * ", "a");
sArray = richTextBox1.Text.Split(‘a‘);
first_number = float.Parse(sArray[0]);
second_number = float.Parse(sArray[1]);
result = first_number * second_number;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
else if (richTextBox1.Text.ToUpper().Contains(" ÷ "))
{
try
{
richTextBox1.Text = richTextBox1.Text.Replace(" ÷ ", "a");
sArray = richTextBox1.Text.Split(‘a‘);
first_number = float.Parse(sArray[0]);
second_number = float.Parse(sArray[1]);
result = first_number / second_number;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
richTextBox1.Text = result.ToString();
busy = 0; sub = 0;
}
}

private void button11_Click(object sender, EventArgs e)
{
if ((busy == 2) || (busy == 3)) { richTextBox1.Text += "0"; busy = 3; }
else{ richTextBox1.Text += "0"; busy = 0; sub = 0; }
}

private void button12_Click(object sender, EventArgs e)
{
if ((busy == 2)||(busy == 3)) { }
else { richTextBox1.Text += "."; busy = 2; sub = 1; }
}

private void button13_Click(object sender, EventArgs e)
{
if (richTextBox1.Text.Length > 0) {
richTextBox1.Text = "";
busy = 1;
sub = 0;
sArray = null;
first_number = 0;
second_number = 0;
result = 0;
judge = 0;
}
}

private void button18_Click(object sender, EventArgs e)
{
if (richTextBox1.Text.Length > 0) {
richTextBox1.Text = richTextBox1.Text.Remove(richTextBox1.Text.Length - 1, 1);
//busy = 1;
}
}

private void richTextBox1_TextChanged(object sender, EventArgs e)
{

}
}
}

winform简易计算器

标签:spl   ide   int start   mes   man   object   小数   pen   kernel32   

原文地址:https://www.cnblogs.com/Lee597/p/14962532.html


评论


亲,登录后才可以留言!