实例介绍
【实例简介】
【实例截图】
【核心代码】
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO; namespace Counter { public partial class Form1 : Form { string NowData = DateTime.Now.ToLongDateString(); string NowTime = DateTime.Now.ToLongTimeString().ToString(); string Q; double NewTB; double QQ; bool Tab1 = true; //判定光标位置,TB_1为TRUE,TB_3为FALSE public Form1() { InitializeComponent(); System.Diagnostics.Trace.Listeners.Clear(); System.Diagnostics.Trace.Listeners.Add(new System.Diagnostics.TextWriterTraceListener(NowData "-Log.txt")); System.Diagnostics.Trace.AutoFlush = true; } private void Form1_Load(object sender, EventArgs e) { System.Diagnostics.Trace.WriteLine(NowTime "-软件重启"); } public void addNum(string num) { if (TB_1.Text!=""&&TB_2.Text!="") { TB_3.Text = TB_3.Text num.ToString(); } else { TB_1.Text = TB_1.Text num.ToString(); } } private void BT_DEL_MouseDown(object sender, MouseEventArgs e) { if (Tab1) { if (TB_1.TextLength > 0) { TB_1.Text = TB_1.Text.Substring(0, TB_1.Text.Length - 1); } } else { if (TB_3.TextLength > 0) { TB_3.Text = TB_3.Text.Substring(0, TB_3.Text.Length - 1); } } } private void BT_P_Click(object sender, EventArgs e) { if ( Tab1 == true) { if (TB_1.TextLength < 1) { TB_1.Text = "0."; } else if (TB_1.Text.IndexOf(".") <= 0) { addNum("."); } } if (Tab1 == false) { if (TB_3.TextLength < 1) { TB_3.Text = "0."; if (TB_3.Text.IndexOf(".") <= 0) { addNum("."); } } } } private void BT_0_Click(object sender, EventArgs e) { if (TB_1.Text != "" && TB_2.Text != "" && TB_3.Text != "" && TB_4.Text != "") { TB_2.Text = ""; TB_3.Text = ""; TB_4.Text = ""; TB_1.Text = "0"; } else { addNum("0"); } } private void BT_1_Click(object sender, EventArgs e) { if (TB_1.Text != "" && TB_2.Text != "" && TB_3.Text != "" && TB_4.Text != "") { TB_2.Text = ""; TB_3.Text = ""; TB_4.Text = ""; this.TB_1.Focus(); Tab1 = true; TB_1.Text = "1"; } else { addNum("1"); } } private void BT_2_Click(object sender, EventArgs e) { if (TB_1.Text != "" && TB_2.Text != "" && TB_3.Text != "" && TB_4.Text != "") { TB_2.Text = ""; TB_3.Text = ""; TB_4.Text = ""; this.TB_1.Focus(); Tab1 = true; TB_1.Text = "2"; } else { addNum("2"); } } private void BT_3_Click(object sender, EventArgs e) { if (TB_1.Text != "" && TB_2.Text != "" && TB_3.Text != "" && TB_4.Text != "") { TB_2.Text = ""; TB_3.Text = ""; TB_4.Text = ""; // this.TB_1.Focus(); Tab1 = true; TB_1.Text = "3"; } else { addNum("3"); } } private void BT_4_Click(object sender, EventArgs e) { if (TB_1.Text != "" && TB_2.Text != "" && TB_3.Text != "" && TB_4.Text != "") { TB_2.Text = ""; TB_3.Text = ""; TB_4.Text = ""; this.TB_1.Focus(); Tab1 = true; TB_1.Text = "4"; } else { addNum("4"); } } private void BT_5_Click(object sender, EventArgs e) { if (TB_1.Text != "" && TB_2.Text != "" && TB_3.Text != "" && TB_4.Text != "") { TB_2.Text = ""; TB_3.Text = ""; TB_4.Text = ""; this.TB_1.Focus(); Tab1 = true; TB_1.Text = "5"; } else { addNum("5"); } } private void BT_6_Click(object sender, EventArgs e) { if (TB_1.Text != "" && TB_2.Text != "" && TB_3.Text != "" && TB_4.Text != "") { TB_2.Text = ""; TB_3.Text = ""; TB_4.Text = ""; this.TB_1.Focus(); Tab1 = true; TB_1.Text = "6"; } else { addNum("6"); } } private void BT_7_Click(object sender, EventArgs e) { if (TB_1.Text != "" && TB_2.Text != "" && TB_3.Text != "" && TB_4.Text != "") { TB_2.Text = ""; TB_3.Text = ""; TB_4.Text = ""; this.TB_1.Focus(); Tab1 = true; TB_1.Text = "7"; } else { addNum("7"); } } private void BT_8_Click(object sender, EventArgs e) { if (TB_1.Text != "" && TB_2.Text != "" && TB_3.Text != "" && TB_4.Text != "") { TB_2.Text = ""; TB_3.Text = ""; TB_4.Text = ""; this.TB_1.Focus(); Tab1 = true; TB_1.Text = "8"; } else { addNum("8"); } } private void BT_9_Click(object sender, EventArgs e) { if (TB_1.Text != "" && TB_2.Text != "" && TB_3.Text != "" && TB_4.Text != "") { TB_1.Text = ""; TB_2.Text = ""; TB_3.Text = ""; TB_4.Text = ""; this.TB_1.Focus(); Tab1 = true; addNum("9"); } else { addNum("9"); } } private void toolStripMenuItem1_Click(object sender, EventArgs e) { string dddd = NowData "-Log.txt"; if (File.Exists(dddd)) { System.Diagnostics.Process.Start(NowData "-Log.txt"); } } private void TB_1_KeyPress(object sender, KeyPressEventArgs e) { if (((int)e.KeyChar < 48 || (int)e.KeyChar > 57) && (int)e.KeyChar != 8 && (int)e.KeyChar != 46) e.Handled = true; if (TB_1.Text != "" && TB_2.Text != "" && TB_3.Text != "" && TB_4.Text != "") { TB_1.Text = ""; TB_2.Text = ""; TB_3.Text = ""; TB_4.Text = ""; this.TB_1.Focus(); Tab1 = true; } if ((int)e.KeyChar == 46) //小数点 { if (TB_1.Text.Length <= 0) e.Handled = true; //小数点不能在第一位 else if (TB_1.Text.IndexOf(".") >= 0) //包含一个小数点 { e.Handled = true; } } if ((int)e.KeyChar == 48) //数字0 { if (TB_1.Text == "0") //第一个数为0时 { e.Handled = true; } } } private void TB_3_KeyPress(object sender, KeyPressEventArgs e) { if (((int)e.KeyChar < 48 || (int)e.KeyChar > 57) && (int)e.KeyChar != 8 && (int)e.KeyChar != 46) e.Handled = true; if (TB_1.Text != "" && TB_2.Text != "" && TB_3.Text != "" && TB_4.Text != ""&& e.Handled == false) { TB_1.Text = ""; TB_2.Text = ""; TB_3.Text = ""; TB_4.Text = ""; this.TB_1.Focus(); Tab1 = true; } if ((int)e.KeyChar == 46) //小数点 { if (TB_3.Text.Length <= 0) e.Handled = true; //小数点不能在第一位 else if (TB_1.Text.IndexOf(".") >= 0) //包含一个小数点 { e.Handled = true; } } if ((int)e.KeyChar == 48) //数字0 { if(TB_3.Text == "0") //第一个数为0时 { e.Handled = true; } } } private void BT_DIV_Click(object sender, EventArgs e) { if (TB_4.Text == "") { TB_2.Text = "÷"; this.TB_3.Focus(); Tab1 = false; } else { TB_1.Text = TB_4.Text; TB_2.Text = ""; TB_3.Text = ""; TB_2.Text = "÷"; this.TB_3.Focus(); Tab1 = false; } } private void BT_MUL_Click(object sender, EventArgs e) { if (TB_4.Text == "") { TB_2.Text = "×"; this.TB_3.Focus(); Tab1 = false; } else { TB_1.Text = TB_4.Text; TB_2.Text = ""; TB_3.Text = ""; TB_2.Text = "×"; this.TB_3.Focus(); Tab1 = false; } } private void BT_MIN_Click(object sender, EventArgs e) { if (TB_4.Text == "") { TB_2.Text = "-"; this.TB_3.Focus(); Tab1 = false; } else { TB_1.Text = TB_4.Text; TB_2.Text = ""; TB_3.Text = ""; TB_2.Text = "-"; this.TB_3.Focus(); Tab1 = false; } } private void BT_ADD_Click(object sender, EventArgs e) { if (TB_4.Text == "") { TB_2.Text = " "; this.TB_3.Focus(); Tab1 = false; } else { TB_1.Text = TB_4.Text; TB_2.Text = ""; TB_3.Text = ""; TB_2.Text = " "; this.TB_3.Focus(); Tab1 = false; } } private void BT_CLE_Click(object sender, EventArgs e) { TB_1.Text = ""; TB_2.Text = ""; TB_3.Text = ""; TB_4.Text = ""; TB_5.Text = ""; this.TB_1.Focus(); Tab1 = true; } private void BT_EQ_Click(object sender, EventArgs e) { try { double X1 = Convert.ToDouble(TB_1.Text); double Y1 = Convert.ToDouble(TB_3.Text); Count W1 = new Count(); Count W2 = new Count(X1, Y1); if (TB_2.Text == " ") { double result = W1.Add(W2); TB_4.Text = Convert.ToString(result); this.TB_1.Focus(); Tab1 = true; System.Diagnostics.Trace.WriteLine(NowTime "——" TB_1.Text TB_2.Text TB_3.Text "=" TB_4.Text); } else if (TB_2.Text == "-") { double result = W1.Min(W2); TB_4.Text = Convert.ToString(result); this.TB_1.Focus(); Tab1 = true; System.Diagnostics.Trace.WriteLine(NowTime "——" TB_1.Text TB_2.Text TB_3.Text "=" TB_4.Text); } else if (TB_2.Text == "×") { double result = W1.Mul(W2); TB_4.Text = Convert.ToString(result); this.TB_1.Focus(); Tab1 = true; System.Diagnostics.Trace.WriteLine(NowTime "——" TB_1.Text TB_2.Text TB_3.Text "=" TB_4.Text); } else if (TB_2.Text == "÷") { double result = W1.Div(W2); TB_4.Text = Convert.ToString(result); this.TB_1.Focus(); Tab1 = true; System.Diagnostics.Trace.WriteLine(NowTime "——" TB_1.Text TB_2.Text TB_3.Text "=" TB_4.Text); } else { MessageBox.Show("无算术符"); } } catch(Exception Error) { System.Diagnostics.Trace.WriteLine(NowTime "-" Error.Message); MessageBox.Show("输入有误"); } } private void Form1_KeyDown(object sender, KeyEventArgs e) { Keys KEY = e.KeyCode; switch (KEY) { case Keys.Enter: BT_EQ_Click(sender, e); e.Handled = true; break; case Keys.Add: BT_ADD_Click(sender, e); e.Handled = true; break; case Keys.Subtract: BT_MIN_Click(sender, e); e.Handled = true; break; case Keys.Multiply: BT_MUL_Click(sender, e); e.Handled = true; break; case Keys.Divide: BT_DIV_Click(sender, e); e.Handled = true; break; case Keys.Space: BT_CN_Click(sender, e); e.Handled = true; break; case Keys.ShiftKey: BT_CLE_Click(sender, e); e.Handled = true; break; default: break; } } private void BT_CN_Click(object sender, EventArgs e) { if (TB_4.Text != "") { NewTB = Convert.ToDouble(TB_4.Text); } else if (TB_1.Text != "") { NewTB = Convert.ToDouble(TB_1.Text); } if (NewTB >= 0 && NewTB - (int)NewTB == 0 && NewTB < 100000000) //无小数点 { Q = Convert.ToString(NewTB); if (Q != null) { if (NewTB >= 0 && NewTB < 100000000) { ToCn W1 = new ToCn(); ToCn W2 = new ToCn(Q); string W = W1.Text(W2); TB_5.Text = Convert.ToString(W); System.Diagnostics.Trace.WriteLine(NowTime "-" TB_5.Text); } } } if (NewTB >= 0 && NewTB - (int)NewTB != 0 && NewTB < 100000000) //有小数点 { NewTB = (Int64)(NewTB * 100 0.5); //四舍五入 Q = Convert.ToString(NewTB); if (NewTB >= 0 && NewTB < 100000000) { ToCn W1 = new ToCn(); ToCn W2 = new ToCn(Q); string W = W1.Text2(W2); TB_5.Text = Convert.ToString(W); System.Diagnostics.Trace.WriteLine(NowTime "-" TB_5.Text); } } else if (NewTB < 0) { MessageBox.Show("数值太小"); } else if (NewTB >= 100000000) { MessageBox.Show("数值太大"); } } private void toolStripMenuItem2_Click(object sender, EventArgs e) { new Form2().Show(); } private void TB_5_DoubleClick(object sender, EventArgs e) { if (TB_5.Text != "") { Clipboard.SetText(TB_5.Text.Trim()); } } private void TB_1_MouseClick(object sender, MouseEventArgs e) { this.TB_1.Focus(); Tab1 = true; } private void TB_3_MouseClick(object sender, MouseEventArgs e) { this.TB_3.Focus(); Tab1 = false; } } }
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论