Control settings
- Drag the textbutton control to display
- Drag the Button control to form a numeric and symbol keyboard,
controls | property name | attribute value | illustrate |
---|---|---|---|
TextBox | ID | txtDisplay | The programmatic name of the textbox control used to display the entered number |
ReadOnly | True | The text in the textbox cannot be changed, the default value is False | |
Botton | Set ID and Text separately | Set to btnOne and number 1 respectively | Represents the programmed name of the "Number 1" button and the text displayed on the "Number 1" button, respectively |
Botton | Set ID and Text separately | Set to btnTwo and number 2 respectively | Represents the programmed name of the "Number 2" button and the text displayed on the "Number 2" button, respectively |
Botton | Set ID and Text separately | Set to btnThree and number 3 respectively | Represents the programmed name of the "Number 3" button and the text displayed on the "Number 3" button, respectively |
Botton | Set ID and Text separately | Set to btnAdd and the symbol "+" respectively | Represents the programmed name of the "+" button and the text displayed on the "+" button, respectively |
Botton | Set ID and Text separately | Set to btnSubtract and the symbol "-" respectively | Represents the programmed name of the "+" button and the text displayed on the "-" button, respectively |
Botton | Set ID and Text separately | Set to btnEqual and symbol "=" respectively | Represents the programmed name of the "=" button and the text displayed on the "=" button, respectively |
After adding, you can manually drag the control to adjust the size, the effect is as follows:
The source code of the form tag is:
<form id="form1" runat="server" style="text-align:center">
<div style="height: 150px; width: 293px; background-color: #FF0000;">
简易计算器<br />
<asp:TextBox ID="txtDisplay" runat="server" Width="180px" Height="30px" style="margin-left: 0px"></asp:TextBox><br />
<asp:Button ID="btnone" runat="server" Text="1" OnClick="btnone_Click" Width="60px" Height="40px" style="margin-left: 0px" />
<asp:Button ID="btnTwo" runat="server" Text="2" OnClick="btnTwo_Click" Width="60px" Height="40px" />
<asp:Button ID="btnThree" runat="server" Text="3" OnClick="btnThree_Click" Width="60px" Height="40px" /><br />
<asp:Button ID="btnAdd" runat="server" Text="+" OnClick="btnAdd_Click" Width="60px" Height="40px" />
<asp:Button ID="btnSubtract" runat="server" Text="-" OnClick="btnSubtract_Click" Width="60px" Height="40px" />
<asp:Button ID="btnEqual" runat="server" Text="=" OnClick="btnEqual_Click" Width="60px" Height="40px" />
</div>
</form>
write computational code
public partial class WebForm1 : System.Web.UI.Page
{
static string num1 = "0", num2 = "0", total = "", sign = "";
// 数字1按钮
protected void btnone_Click(object sender, EventArgs e)
{
total += "1";
txtDisplay.Text = total;
}
// 数字2按钮
protected void btnTwo_Click(object sender, EventArgs e)
{
total += "2";
txtDisplay.Text = total;
}
// 数字3 按钮
protected void btnThree_Click(object sender, EventArgs e)
{
total += "3";
txtDisplay.Text = total;
}
// 加法按钮
protected void btnAdd_Click(object sender, EventArgs e)
{
if (sign.Length == 1)
{
Count();
num1 = txtDisplay.Text;
sign = "+";
}
else
{
num1 = txtDisplay.Text;
txtDisplay.Text = "";
total = "";
sign = "+";
}
}
//减法按钮
protected void btnSubtract_Click(object sender, EventArgs e)
{
if (sign.Length == 1)
{
Count();
num1 = txtDisplay.Text;
sign = "-";
}
else
{
num1 = txtDisplay.Text;
txtDisplay.Text = "";
total = "";
sign = "-";
}
}
// 等于按钮
protected void btnEqual_Click(object sender, EventArgs e)
{
Count();
}
// 函数定义
protected void Count()
{
num2 = txtDisplay.Text;
if (num2 == "")
{
num2 = "0";
}
switch (sign)
{
case "+":
txtDisplay.Text = (int.Parse(num1) + int.Parse(num2)).ToString();
num1 = "0";
num2 = "0";
total = "";
sign = "";
break;
case "-":
txtDisplay.Text = (int.Parse(num1) - int.Parse(num2)).ToString();
num1 = "0";
num2 = "0";
total = "";
sign = "";
break;
}
}
test