---------------------- <a href="http://net.itheima.com/" target="blank">Windows Phone 7手机开发</a>、<a href="http://net.itheima.com/" target="blank">.Net培训</a>、期待与您交流! ----------------------
这个是我看了学习视频以后,练习了一下。这是一个计算器代码,请多多指教。
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;
namespace Calculatortest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
/// <summary>
/// /点击=时,计算前面的值
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button10_Click(object sender, EventArgs e)
{
double a = Convert.ToDouble(this.textBox1.Text); //获取textbox1的值
double b = Convert.ToDouble(this.textBox2.Text); //获取textbox2的值
//判断textbox4文本框的值是否为空
if (this.textBox4.Text == "")
{
MessageBox.Show("运算符不能为空!");
textBox4.Focus();
}
else
{
//判断textbox4文本框的值是+、-、*、/
if (this.textBox4.Text == "+")
{
this.textBox3.Text = Convert.ToString(a + b);
}
else if (this.textBox4.Text == "-")
{
this.textBox3.Text = Convert.ToString(a - b);
}
else if (this.textBox4.Text == "*")
{
this.textBox3.Text = Convert.ToString(a * b);
}
else if (this.textBox4.Text == "/")
{
//判断textbox2的值是否为空
if (b == 0)
{
MessageBox.Show("0不能为除数");
}
else
{
this.textBox3.Text = Convert.ToString(a / b);
}
}
}
}
/// <summary>
/// 点击按钮时,给textbox4文本框赋值+
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void sum1_Click(object sender, EventArgs e)
{
textBox4.Text = "";
this.textBox4.Text += "+";
}
/// <summary>
/// /点击按钮时,给textbox4文本框赋值-
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void sum2_Click(object sender, EventArgs e)
{
textBox4.Text = "";
this.textBox4.Text = "-";
}
/// <summary>
/// 点击按钮时,给textbox4文本框赋值*
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void sum3_Click(object sender, EventArgs e)
{
textBox4.Text = "";
this.textBox4.Text = "*";
}
/// <summary>
/// 点击按钮时,给textbox4文本框赋值/
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void sum4_Click(object sender, EventArgs e)
{
textBox4.Text = "";
this.textBox4.Text = "/";
}
/// <summary>
/// 点击按钮时,给textbox1文本框赋值.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void sum6_Click(object sender, EventArgs e)
{
this.textBox1.Text += sum6.Text;
}
/// <summary>
/// /清空页面上的textbox文本框
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
foreach (Control item in this.Controls)
{
if (item is TextBox)
{
((TextBox)item).Text = "";
}
}
}
/// <summary>
/// 验证textbox1文本框的值是否是数字
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if ((e.KeyChar >= '0' && e.KeyChar <= '9') || ((Keys)e.KeyChar == Keys.Back))
{
e.Handled = false;
}
else
{
e.Handled = true;
MessageBox.Show("只能输入数字");
this.textBox1.Text = "";
}
}
/// <summary>
/// 验证textbox2文本框的值是否是数字
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
{
if ((e.KeyChar >= '0' && e.KeyChar <= '9') || ((Keys)e.KeyChar == Keys.Back))
{
e.Handled = false;
}
else
{
e.Handled = true;
MessageBox.Show("只能输入数字");
this.textBox1.Text = "";
}
}
#endregion
#region 点击number时,获取它的text值
/// <summary>
/// 点击number1时,获取它的text值
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void number1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "0")
{
textBox2.Text += number1.Text;
}
else
{
if (textBox1.Text=="")
{
textBox1.Text += number1.Text;
}
else
{
if (textBox4.Text!="")
{
textBox2.Text += number1.Text;
}
}
}
}
/// <summary>
/// 点击number2时,获取它的text值
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void number2_Click(object sender, EventArgs e)
{
this.textBox1.Text += number2.Text;
if (textBox4.Text != "")
{
textBox2.Focus();
textBox2.Text += number2.Text;
}
}
/// <summary>
/// 点击number3时,获取它的text值
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void number3_Click(object sender, EventArgs e)
{
this.textBox1.Text += number3.Text;
if (textBox4.Text != "")
{
textBox2.Focus();
textBox2.Text += number3.Text;
}
}
/// <summary>
/// 点击number4时,获取它的text值
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void number4_Click(object sender, EventArgs e)
{
this.textBox1.Text += number4.Text;
if (textBox4.Text != "")
{
textBox2.Focus();
textBox2.Text += number4.Text;
}
}
/// <summary>
/// 点击number5时,获取它的text值
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void number5_Click(object sender, EventArgs e)
{
this.textBox1.Text += number5.Text;
if (textBox4.Text != "")
{
textBox2.Focus();
textBox2.Text += number5.Text;
}
}
/// <summary>
/// 点击number6时,获取它的text值
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void number6_Click(object sender, EventArgs e)
{
this.textBox1.Text += number6.Text;
if (textBox4.Text != "")
{
textBox2.Focus();
textBox2.Text += number6.Text;
}
}
/// <summary>
/// 点击number7时,获取它的text值
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void number7_Click(object sender, EventArgs e)
{
this.textBox1.Text += number7.Text;
if (textBox4.Text != "")
{
textBox2.Focus();
textBox2.Text += number7.Text;
}
}
/// <summary>
/// 点击number8时,获取它的text值
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void number8_Click(object sender, EventArgs e)
{
this.textBox1.Text += number8.Text;
if (textBox4.Text != "")
{
textBox2.Focus();
textBox2.Text += number8.Text;
}
}
/// <summary>
/// 点击number9时,获取它的text值
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void number9_Click(object sender, EventArgs e)
{
this.textBox1.Text += number9.Text;
if (textBox4.Text != "")
{
textBox2.Focus();
textBox2.Text += number9.Text;
}
}
/// <summary>
/// 点击number0时,获取它的text值
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void number0_Click(object sender, EventArgs e)
{
if (textBox1.Text=="0")
{
this.textBox1.Text = number0.Text;
}
else
{
textBox1.Text += number0.Text;
if (textBox4.Text != "")
{
textBox2.Focus();
textBox2.Text += number0.Text;
}
}
}
#endregion
}
}
---------------------- <a href="
http://net.itheima.com/" target="blank">Windows Phone 7手机开发</a>、<a href="
http://net.itheima.com/" target="blank">.Net培训</a>、期待与您交流! ----------------------
---------------------- <a href=" http://net.itheima.com/" target="blank">Windows Phone 7手机开发</a>、<a href=" http://net.itheima.com/" target="blank">.Net培训</a>、期待与您交流! ----------------------