Make a calculartor in c sharp
- Create an new windwos forms project.
- Design Size.
- Add panel.
design panel color,size,style. - Add button.
calculartor button and oper button. - Coding button function.
Declaring variables
double fstNum,secNum;
string oper;
Identify button function
btnCE
private void btnCE_Click(object sender, EventArgs e)
{
textDisplay.Text = "0";
string f, s;
f = Convert.ToString(fstNum);
s = Convert.ToString(secNum);
f = "";
s = "";
}
backspace
private void backspace_Click(object sender, EventArgs e)
{
if (textDisplay.Text.Length > 0)
{
textDisplay.Text = textDisplay.Text.Remove(textDisplay.Text.Length - 1, 1);
}
if (textDisplay.Text == "")
{
textDisplay.Text = "0";
}
}
btn9
private void btn9_Click(object sender, EventArgs e)
{
if (textDisplay.Text == "0")
{
textDisplay.Text = "9";
}
else
{
textDisplay.Text = textDisplay.Text + "9";
}
}
btnAdd
private void btnAdd_Click(object sender, EventArgs e)
{
fstNum = double.Parse(textDisplay.Text);
oper = "+";
textDisplay.Text = "";
}
btnPM
private void btnPM_Click(object sender, EventArgs e)
{
double q = Convert.ToDouble(textDisplay.Text);
textDisplay.Text = Convert.ToString(-1 * q);
}
btnC
private void btnC_Click(object sender, EventArgs e)
{
textDisplay.Text = "0";
}
benDec
private void benDec_Click(object sender, EventArgs e)
{
if (benDec.Text == ".")
{
if (!textDisplay.Text.Contains("."))
{
textDisplay.Text = textDisplay.Text + benDec.Text;
}
}
}
use switch statement out put equal
private void btnEuq_Click(object sender, EventArgs e)
{
secNum = double.Parse(textDisplay.Text);
switch (oper)
{
case "+":
textDisplay.Text = (fstNum + secNum).ToString();
break;
case "-":
textDisplay.Text = (fstNum - secNum).ToString();
break;
case "*":
textDisplay.Text = (fstNum * secNum).ToString();
break;
case "/":
textDisplay.Text = (fstNum / secNum).ToString();
break;
default:
break;
}
}
Code download:
caluclator.zip
评论功能已关闭