先入正题吧!
1:创建wpf应用。我用的VS是2013!
2:创建wpf文件并且起名字!
3:起一条虚线
3:打开我们电脑基本的计算器,然后我们模仿他来做计算器吧!
4:在工具箱找到lable,把他拖进来。并且起一个名字!设置他的属性。
4:再然后在工具箱找到button按钮把他拖进来。并且帮他起名字,还有设置他的属性!
5:写完整的界面,按钮直接复制粘贴就可以的!下一步进行后台代码!
6:定义三个变量存放数据!
7:设置按钮0的点击事件!然后数字键复制粘贴就可以了!
8:设置符号键,给他赋值!
9:设置C按键清除所有!这个问了很多人才找到的C按键清除lable!
10:设置等于号按键促发加减乘除!
11:然后一个计算器完成了!下面是全部代码!
namespace LVcalculation
{
///
/// MainWindow.xaml 的交互逻辑
///
public partial class MainWindow : Window
{
string number1 = null, number2 = null,flag=null;//第一个变量存放按下的第一个字符,第二个变量存放按下符号键的字符。第三个存放符号的字符!
public MainWindow()
{
InitializeComponent();
}
private void button0_Click(object sender, RoutedEventArgs e)
{
if (flag == null)
{
number1 = number1 + "0";
lable1.Content = number1;
}
else {
number2 = number2 + "0";
lable1.Content = number2;
}
}
private void button1_Click(object sender, RoutedEventArgs e)
{
if (flag == null)
{
number1 = number1 + "1";
lable1.Content = number1;
}
else
{
number2 = number2 + "1";
lable1.Content = number2;
}
}
private void button2_Click(object sender, RoutedEventArgs e)
{
if (flag == null)
{
number1 = number1 + "2";
lable1.Content = number1;
}
else
{
number2 = number2 + "2";
lable1.Content = number2;
}
}
private void button3_Click(object sender, RoutedEventArgs e)
{
if (flag == null)
{
number1 = number1 + "3";
lable1.Content = number1;
}
else
{
number2 = number2 + "3";
lable1.Content = number2;
}
}
private void button4_Click(object sender, RoutedEventArgs e)
{
if (flag == null)
{
number1 = number1 + "4";
lable1.Content = number1;
}
else
{
number2 = number2 + "4";
lable1.Content = number2;
}
}
private void button5_Click(object sender, RoutedEventArgs e)
{
if (flag == null)
{
number1 = number1 + "5";
lable1.Content = number1;
}
else
{
number2 = number2 + "5";
lable1.Content = number2;
}
}
private void button6_Click(object sender, RoutedEventArgs e)
{
if (flag == null)
{
number1 = number1 + "6";
lable1.Content = number1;
}
else
{
number2 = number2 + "6";
lable1.Content = number2;
}
}
private void button7_Click(object sender, RoutedEventArgs e)
{
if (flag == null)
{
number1 = number1 + "7";
lable1.Content = number1;
}
else
{
number2 = number2 + "7";
lable1.Content = number2;
}
}
private void button8_Click(object sender, RoutedEventArgs e)
{
if (flag == null)
{
number1 = number1 + "8";
lable1.Content = number1;
}
else
{
number2 = number2 + "8";
lable1.Content = number2;
}
}
private void button9_Click(object sender, RoutedEventArgs e)
{
if (flag == null)
{
number1 = number1 + "9";
lable1.Content = number1;
}
else
{
number2 = number2 + "9";
lable1.Content = number2;
}
}
private void buttonb_Click(object sender, RoutedEventArgs e)
{
flag = "-";
}
private void buttona_Click(object sender, RoutedEventArgs e)
{
flag = "+";
}
private void buttone_Click(object sender, RoutedEventArgs e)
{
flag = "%";
}
private void buttond_Click(object sender, RoutedEventArgs e)
{
flag = "/";
}
private void buttonc_Click(object sender, RoutedEventArgs e)
{
flag = "*";
}
private void buttoncc_Click(object sender, RoutedEventArgs e)
{
this.lable1.Content = "";
}
private void buttongg_Click(object sender, RoutedEventArgs e)
{
if (flag == null)
{
number1 = number1 + ".";
lable1.Content = number1;
}
else {
number2 = number2 + ".";
lable1.Content = number2;
}
}
private void buttonf_Click(object sender, RoutedEventArgs e)
{
switch(flag){
case "+": lable1.Content= Convert.ToString(Convert.ToDouble(number1) + Convert.ToDouble(number2));
break;
case "-": lable1.Content = Convert.ToString(Convert.ToDouble(number1) - Convert.ToDouble(number2));
break;
case "*": lable1.Content = Convert.ToString(Convert.ToDouble(number1) * Convert.ToDouble(number2));
break;
case "/": lable1.Content = Convert.ToString(Convert.ToDouble(number1) / Convert.ToDouble(number2));
break;
case "%": lable1.Content = Convert.ToString(Convert.ToDouble(number1) % Convert.ToDouble(number2));
break;
}
number1 = null;
number2 = null;
flag = null;
}
}
}
12:还有一种方法写!注意这个方法的是用textbox,不是用lable。
13:下面是这个的全部代码!
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="为什么我写的计算器这么多bug" Height="350" MaxHeight="350" MinHeight="350" Width="525" MaxWidth="525" MinWidth="525"
>
namespace WpfApplication1
{
///
/// MainWindow.xaml 的交互逻辑
///
public partial class MainWindow : Window
{
double a;//定义为存储结果。
double b;//定义为第一位操作符。
bool c = false;
string d;//定义为操作运算符号。
public MainWindow()
{
InitializeComponent();
}
private void buttonCC_Click(object sender, RoutedEventArgs e)
{
textbox1.Text = "";
}
private void button0_Click(object sender, RoutedEventArgs e)
{
if(c==true){
textbox1.Text = "";
c = false;
}
textbox1.Text += "0";
}
private void button1_Click(object sender, RoutedEventArgs e)
{
if(c==true){
textbox1.Text = "";
c = false;
}
textbox1.Text += "1";
}
private void button2_Click(object sender, RoutedEventArgs e)
{
if (c == true)
{
textbox1.Text = "";
c = false;
}
textbox1.Text += "2";
}
private void button3_Click(object sender, RoutedEventArgs e)
{
if (c == true)
{
textbox1.Text = "";
c = false;
}
textbox1.Text += "3";
}
private void button4_Click(object sender, RoutedEventArgs e)
{
if (c == true)
{
textbox1.Text = "";
c = false;
}
textbox1.Text += "4";
}
private void button5_Click(object sender, RoutedEventArgs e)
{
if (c == true)
{
textbox1.Text = "";
c = false;
}
textbox1.Text += "5";
}
private void button6_Click(object sender, RoutedEventArgs e)
{
if (c == true)
{
textbox1.Text = "";
c = false;
}
textbox1.Text += "6";
}
private void button7_Click(object sender, RoutedEventArgs e)
{
if (c == true)
{
textbox1.Text = "";
c = false;
}
textbox1.Text += "7";
}
private void button8_Click(object sender, RoutedEventArgs e)
{
if (c == true)
{
textbox1.Text = "";
c = false;
}
textbox1.Text += "8";
}
private void button9_Click(object sender, RoutedEventArgs e)
{
if (c == true)
{
textbox1.Text = "";
c = false;
}
textbox1.Text += "9";
}
private void buttonc_Click(object sender, RoutedEventArgs e)
{
c = true;
b = double.Parse(textbox1.Text);
d = "*";
}
private void buttona_Click(object sender, RoutedEventArgs e)
{
c = true;
b = double.Parse(textbox1.Text);
d = "+";
}
private void buttonb_Click(object sender, RoutedEventArgs e)
{
c = true;
b = double.Parse(textbox1.Text);
d = "-";
}
private void buttond_Click(object sender, RoutedEventArgs e)
{
c = true;
b = double.Parse(textbox1.Text);
d = "/";
}
private void buttonf_Click(object sender, RoutedEventArgs e)
{
c = true;
b = double.Parse(textbox1.Text);
d = "%";
}
private void buttone_Click(object sender, RoutedEventArgs e)
{
switch(d){
case "+": a = b + double.Parse(textbox1.Text);
break;
case "-": a = b - double.Parse(textbox1.Text);
break;
case "*": a = b * double.Parse(textbox1.Text);
break;
case "/": a = b /double.Parse(textbox1.Text);
break;
case "%": a = b % double.Parse(textbox1.Text);
break;
}
textbox1.Text = a + "";
c = true;
}
}
}
根据大家的需求。以前写的计算器是不可以连续计算的。现在已经更改可以连续计算了。只是添加了几个判断。很简单的。
这个是代码,有注释哦。
第一:在计算器的数字进行判断。
第二:在等号的方法进行判断。超级简单易懂。
有需要源码的也可以联系我。有一次满足的大众的需求。哈哈哈哈哈~~~~
这个是项目的源码下载地:https://download.csdn.net/download/weixin_42009898/11193679
如果有不足的地方欢迎多多指教!记得我是一个热爱编程的男孩子!有错误的地方多多指点!我是一个刚刚毕业入门的应届生!希望我们一起进步!有问题可以咨询我!欢迎给我留言!