历时一个晚上,终于搞定了第一个C#应用程序。虽然做的很简陋,但是毕竟第一次,心情还是蛮激动的。
做完之后又学习了一下如何做安装包,因为觉得做成安装包更酷一点。但是做完之后发现安装包需要一个
安装向导才能完成安装,我想可能安装包有的地方做的不够到位吧,等以后在回来改进。
由于制作比较简单,这里就只附上程序代码啦:
using System;
using System.Windows.Forms;
namespace calculate1
{
public partial class Form1 : Form
{
private double _num1 = 0, _num2 = 0, _result = 0;
private string _operator = "";
private int _operatorCount = 0;
private bool _firstNumberFlag = false;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (_firstNumberFlag == true)
{
textBox1.Text = "2";
_firstNumberFlag = false;
}
else
textBox1.Text = textBox1.Text + "2";
textBox1.Focus();
textBox1.Select(textBox1.Text.Length, 0);
}
private void buttonNum0_Click(object sender, EventArgs e)
{
if (_firstNumberFlag == true)
{
textBox1.Text = "0";
_firstNumberFlag = false;
}
else
textBox1.Text = textBox1.Text + "0";
textBox1.Focus();
textBox1.Select(textBox1.Text.Length, 0);
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void buttonNum3_Click(object sender, EventArgs e)
{
if (_firstNumberFlag == true)
{
textBox1.Text = "3";
_firstNumberFlag = false;
}
else
textBox1.Text = textBox1.Text + "3";
textBox1.Focus();
textBox1.Select(textBox1.Text.Length, 0);
}
private void buttonNum4_Click(object sender, EventArgs e)
{
if (_firstNumberFlag == true)
{
textBox1.Text = "4";
_firstNumberFlag = false;
}
else
textBox1.Text = textBox1.Text + "4";
textBox1.Focus();
textBox1.Select(textBox1.Text.Length, 0);
}
private void buttonNum5_Click(object sender, EventArgs e)
{
if (_firstNumberFlag == true)
{
textBox1.Text = "5";
_firstNumberFlag = false;
}
else
textBox1.Text = textBox1.Text + "5";
textBox1.Focus();
textBox1.Select(textBox1.Text.Length, 0);
}
private void buttonNum6_Click(object sender, EventArgs e)
{
if (_firstNumberFlag == true)
{
textBox1.Text = "6";
_firstNumberFlag = false;
}
else
textBox1.Text = textBox1.Text + "6";
textBox1.Focus();
textBox1.Select(textBox1.Text.Length, 0);
}
private void buttonNum7_Click(object sender, EventArgs e)
{
if (_firstNumberFlag == true)
{
textBox1.Text = "7";
_firstNumberFlag = false;
}
else
textBox1.Text = textBox1.Text + "7";
textBox1.Focus();
textBox1.Select(textBox1.Text.Length, 0);
}
private void buttonNum8_Click(object sender, EventArgs e)
{
if (_firstNumberFlag == true)
{
textBox1.Text = "8";
_firstNumberFlag = false;
}
else
textBox1.Text = textBox1.Text + "8";
textBox1.Focus();
textBox1.Select(textBox1.Text.Length, 0);
}
private void buttonNum9_Click(object sender, EventArgs e)
{
if (_firstNumberFlag == true)
{
textBox1.Text = "9";
_firstNumberFlag = false;
}
else
textBox1.Text = textBox1.Text + "9";
textBox1.Focus();
textBox1.Select(textBox1.Text.Length, 0);
}
private void buttonSubtract_Click(object sender, EventArgs e)
{
_firstNumberFlag = true;
_operatorCount = _operatorCount + 1;
if (_operatorCount == 1)
{
_num1 = Convert.ToDouble(textBox1.Text);
_operator = "-";
textBox1.Text = "";
}
else
{
_num2 = Convert.ToDouble(textBox1.Text);
if (_operator == "+")
_num1 = _num1 + _num2;
else if (_operator == "-")
_num1 = _num1 - _num2;
else if (_operator == "*")
_num1 = _num1 * _num2;
else
_num1 = _num1 / _num2;
_operator = "-";
textBox1.Text = _num1.ToString();
}
textBox1.Focus();
textBox1.Select(textBox1.Text.Length, 0);
}
private void buttonMultiply_Click(object sender, EventArgs e)
{
_firstNumberFlag = true;
_operatorCount = _operatorCount + 1;
if (_operatorCount == 1)
{
_num1 = Convert.ToDouble(textBox1.Text);
_operator = "*";
textBox1.Text = "";
}
else
{
_num2 = Convert.ToDouble(textBox1.Text);
if (_operator == "+")
_num1 = _num1 + _num2;
else if (_operator == "-")
_num1 = _num1 - _num2;
else if (_operator == "*")
_num1 = _num1 * _num2;
else
_num1 = _num1 / _num2;
_operator = "*";
textBox1.Text = _num1.ToString();
}
textBox1.Focus();
textBox1.Select(textBox1.Text.Length, 0);
}
private void buttonDivide_Click(object sender, EventArgs e)
{
_firstNumberFlag = true;
_operatorCount = _operatorCount + 1;
if (_operatorCount == 1)
{
_num1 = Convert.ToDouble(textBox1.Text);
_operator = "/";
textBox1.Text = "";
}
else
{
_num2 = Convert.ToDouble(textBox1.Text);
if (_operator == "+")
_num1 = _num1 + _num2;
else if (_operator == "-")
_num1 = _num1 - _num2;
else if (_operator == "*")
_num1 = _num1 * _num2;
else
_num1 = _num1 / _num2;
_operator = "/";
textBox1.Text = _num1.ToString();
}
textBox1.Focus();
textBox1.Select(textBox1.Text.Length, 0);
}
private void buttonEqual_Click(object sender, EventArgs e)
{
_num2 = Convert.ToDouble(textBox1.Text);
if (_operator == "+")
_result = _num1 + _num2;
else if (_operator == "-")
_result = _num1 - _num2;
else if (_operator == "*")
_result = _num1 * _num2;
else
_result = _num1 / _num2;
textBox1.Text = _result.ToString();
_operatorCount = 0;
textBox1.Focus();
textBox1.Select(textBox1.Text.Length, 0);
}
private void buttonDot_Click(object sender, EventArgs e)
{
if(_firstNumberFlag==true)
{
textBox1.Text = ".";
_firstNumberFlag = false;
}
else
{
if (textBox1.Text.IndexOf('.') == -1)
textBox1.Text = textBox1.Text + ".";
}
textBox1.Focus();
textBox1.Select(textBox1.Text.Length, 0);
}
private void buttonBackSpace_Click(object sender, EventArgs e)
{
if (textBox1.Text.Length > 0)
textBox1.Text = textBox1.Text.Substring(0, textBox1.Text.Length - 1);
textBox1.Focus();
textBox1.Select(textBox1.Text.Length, 0);
}
private void buttonClear_Click(object sender, EventArgs e)
{
_num1 = 0;_num2 = 0;_result = 0;
_operator = "";
_operatorCount = 0;
_firstNumberFlag = false;
textBox1.Text = "";
textBox1.Focus();
textBox1.Select(textBox1.Text.Length, 0);
}
private void buttonSign_Click(object sender, EventArgs e)
{
if(textBox1.Text.Length>=1)
{
string firstChar = textBox1.Text.Substring(0, 1);
if (firstChar == "-")
textBox1.Text = "+" + textBox1.Text.Substring(1, textBox1.Text.Length - 1);
else if (firstChar == "+")
textBox1.Text = "-" + textBox1.Text.Substring(1, textBox1.Text.Length - 1);
else
textBox1.Text = "-" + textBox1.Text;
}
textBox1.Focus();
textBox1.Select(textBox1.Text.Length, 0);
}
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == '0')
buttonNum0_Click(sender, e);
else if (e.KeyChar == '1')
buttonNum1_Click(sender, e);
else if (e.KeyChar == '2')
button1_Click(sender, e);
else if (e.KeyChar == '3')
buttonNum3_Click(sender, e);
else if (e.KeyChar == '4')
buttonNum4_Click(sender, e);
else if (e.KeyChar == '5')
buttonNum5_Click(sender, e);
else if (e.KeyChar == '6')
buttonNum6_Click(sender, e);
else if (e.KeyChar == '7')
buttonNum7_Click(sender, e);
else if (e.KeyChar == '8')
buttonNum8_Click(sender, e);
else if (e.KeyChar == '9')
buttonNum9_Click(sender, e);
else if (e.KeyChar == '+')
buttonAdd_Click(sender, e);
else if (e.KeyChar == '-')
buttonSubtract_Click(sender, e);
else if (e.KeyChar == '*')
buttonMultiply_Click(sender, e);
else if (e.KeyChar == '/')
buttonDivide_Click(sender, e);
else if (e.KeyChar == '=')
buttonEqual_Click(sender, e);
else if (e.KeyChar == 'c' || e.KeyChar == 'C')
buttonClear_Click(sender, e);
else if (e.KeyChar == '.')
buttonDot_Click(sender, e);
else if (e.KeyChar == 8)
buttonBackSpace_Click(sender, e);
textBox1.Select(textBox1.Text.Length, 0);
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void buttonAdd_Click(object sender, EventArgs e)
{
_firstNumberFlag =true;
_operatorCount = _operatorCount + 1;
if(_operatorCount==1)
{
_num1 = Convert.ToDouble(textBox1.Text);
_operator = "+";
textBox1.Text = "";
}
else
{
_num2 = Convert.ToDouble(textBox1.Text);
if (_operator == "+")
_num1 = _num1 + _num2;
else if (_operator == "-")
_num1 = _num1 - _num2;
else if (_operator == "*")
_num1 = _num1 * _num2;
else
_num1 = _num1 / _num2;
_operator = "+";
textBox1.Text = _num1.ToString();
}
textBox1.Focus();
textBox1.Select(textBox1.Text.Length, 0);
}
private void buttonNum1_Click(object sender, EventArgs e)
{
if (_firstNumberFlag == true)
{
textBox1.Text = "1";
_firstNumberFlag = false;
}
else
textBox1.Text = textBox1.Text + "1";
textBox1.Focus();
textBox1.Select(textBox1.Text.Length, 0);
}
}
}