首先布局出计算器的界面,
并添加每一个按钮的单击事件。单击数字按钮的事件为:
private void button1_Click(object sender, System.EventArgs e)
{
if(textBox1.Text == "0")
{
textBox1.Text = "1"; //此数字的符号
}
else
{
if(flag_count) //记录算术符号(如'+')是否被单击,如果单击则清空现有文本框
{
textBox1.Text = "";
flag_count = false;
}
textBox1.Text = textBox1.Text + "1";
}
}
单击负号的事件为:
private void buttonMinus_Click(object sender, System.EventArgs e)
{
if(textBox1.Text[0] == '-')
{
textBox1.Text = textBox1.Text.Remove(0,1);
}
else
{
textBox1.Text = "-" + textBox1.Text;
}
}单击小数点的事件为:
private void button11_Click(object sender, System.EventArgs e)
{
if(!flag_point)
{
textBox1.Text = textBox1.Text + ".";
flag_point = true;
}
}
并添加每一个按钮的单击事件。单击数字按钮的事件为:
private void button1_Click(object sender, System.EventArgs e)
{
if(textBox1.Text == "0")
{
textBox1.Text = "1"; //此数字的符号
}
else
{
if(flag_count) //记录算术符号(如'+')是否被单击,如果单击则清空现有文本框
{
textBox1.Text = "";
flag_count = false;
}
textBox1.Text = textBox1.Text + "1";
}
}
单击负号的事件为:
private void buttonMinus_Click(object sender, System.EventArgs e)
{
if(textBox1.Text[0] == '-')
{
textBox1.Text = textBox1.Text.Remove(0,1);
}
else
{
textBox1.Text = "-" + textBox1.Text;
}
}单击小数点的事件为:
private void button11_Click(object sender, System.EventArgs e)
{
if(!flag_point)
{
textBox1.Text = textBox1.Text + ".";
flag_point = true;
}
}
并添加每一个按钮的单击事件。单击数字按钮的事件为:
private void button1_Click(object sender, System.EventArgs e)
{
if(textBox1.Text == "0")
{
textBox1.Text = "1"; //此数字的符号
}
else
{
if(flag_count) //记录算术符号(如'+')是否被单击,如果单击则清空现有文本框
{
textBox1.Text = "";
flag_count = false;
}
textBox1.Text = textBox1.Text + "1";
}
}
单击负号的事件为:
private void buttonMinus_Click(object sender, System.EventArgs e)
{
if(textBox1.Text[0] == '-')
{
textBox1.Text = textBox1.Text.Remove(0,1);
}
else
{
textBox1.Text = "-" + textBox1.Text;
}
}单击小数点的事件为:
private void button11_Click(object sender, System.EventArgs e)
{
if(!flag_point)
{
textBox1.Text = textBox1.Text + ".";
flag_point = true;
}
}