日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

一个用WPF做的简单计算器源代码

發(fā)布時間:2025/4/16 asp.net 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 一个用WPF做的简单计算器源代码 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

一、界面設計XAML代碼

<Window x:Class="fengjisuanqi.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"Title="我的簡單計算器" Height="700" Width="480" ResizeMode="CanMinimize"><Grid Margin="0,0,0,0"><Grid.ColumnDefinitions><ColumnDefinition/><ColumnDefinition/><ColumnDefinition/><ColumnDefinition/></Grid.ColumnDefinitions><Grid.RowDefinitions><RowDefinition Height="3*"/><RowDefinition Height="*"/><RowDefinition Height="2*"/><RowDefinition Height="2*"/><RowDefinition Height="2*"/><RowDefinition Height="2*"/></Grid.RowDefinitions><!--<StackPanel Grid.Row="0" Grid.ColumnSpan="4" Margin="0"><TextBlock Name="gongshi" Height="Auto" Margin="0,10,0,0" Background="#FF5D4D4D" HorizontalAlignment="Right" FontSize="36" Width="470" Foreground="White"/><TextBlock Name="jiguo" Height="80" Margin="0" Background="#ff5d4d4d" HorizontalAlignment="Right" Width="470" FontSize="36" Foreground="White"></TextBlock></StackPanel>--><TextBlock Name="ss" Grid.Row="0" Grid.ColumnSpan="4" Width="470" Height="Auto" Margin="0,0,0,0" Background="#FF5D4D4D" HorizontalAlignment="Right" FontSize="36" Foreground="White"/><TextBlock Name="gongshi" Grid.Row="0" Grid.ColumnSpan="4" Height="Auto" Margin="0,0,0,0" Background="#FF5D4D4D" HorizontalAlignment="Right" FontSize="36" Foreground="White"/><TextBlock Name="jiguo" Grid.Row="0" Grid.ColumnSpan="4" Height="60" Margin="0" Background="#FF5D4D4D" HorizontalAlignment="Right" FontSize="36" Foreground="White"/><Button Content="7" FontSize="50" Grid.Row="2" Grid.Column="0" Click="Button_Click_1" Name="B7" /><Button Content="8" FontSize="50" Grid.Row="2" Grid.Column="1" Click="Button_Click_1" Name="B8" /><Button Content="9" FontSize="50" Grid.Row="2" Grid.Column="2" Click="Button_Click_1" Name="B9" /><Button Content="4" FontSize="50" Grid.Row="3" Grid.Column="0" Click="Button_Click_1" Name="B4" /><Button Content="5" FontSize="50" Grid.Row="3" Grid.Column="1" Click="Button_Click_1" Name="B5" /><Button Content="6" FontSize="50" Grid.Row="3" Grid.Column="2" Click="Button_Click_1" Name="B6" /><Button Content="1" FontSize="50" Grid.Row="4" Grid.Column="0" Click="Button_Click_1" Name="B1" /><Button Content="2" FontSize="50" Grid.Row="4" Grid.Column="1" Click="Button_Click_1" Name="B2" /><Button Content="3" FontSize="50" Grid.Row="4" Grid.Column="2" Click="Button_Click_1" Name="B3" /><Button Content="." FontSize="50" Grid.Row="5" Grid.Column="0" Click="Button_Click_1" Name="B" /><Button Content="0" FontSize="50" Grid.Row="5" Grid.Column="1" Click="Button_Click_1" Name="B0" /><Button Name="divide" FontSize="50" Content="/" Grid.Column="3" Grid.Row="2" Click="fuhao_Click_1"/><Button Name="Multiply" FontSize="50" Content="*" Grid.Column="3" Grid.Row="3" Click="fuhao_Click_1"/><Button Name="Minus" FontSize="50" Content="-" Grid.Column="3" Grid.Row="4" Click="fuhao_Click_1"/><Button Name="Add" FontSize="50" Content="+" Grid.Column="3" Grid.Row="5" Click="fuhao_Click_1"/><Button Name="result" FontSize="50" Grid.Column="2" Grid.Row="5" Click="result_Click_1" Content="="/><Button Name="del" FontSize="30" Grid.Column="3" Grid.Row="1" Content="CE" Click="del_Click_1" Background="#FFEE0E0E"/><Button Name="tuige" FontSize="30" Grid.Column="2" Grid.Row="1" Content="退格" Click="tuige_Click_1"/><Rectangle Name="rect" Grid.Row="1" Grid.ColumnSpan="2"><Rectangle.Fill><LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"><GradientStop Color="#FF9DA49F" Offset="0"/><GradientStop Color="White" Offset="1"/></LinearGradientBrush></Rectangle.Fill></Rectangle></Grid> </Window>

二、效果圖

三、后臺邏輯代碼

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes;namespace fengjisuanqi {/// <summary>/// MainWindow.xaml 的交互邏輯/// </summary>public partial class MainWindow : Window{private string anxiazhi = "";private double resultnum = 0.0;public MainWindow(){InitializeComponent();}//運算方法private void OperationNum(string s){if (jiguo.Text != ""){switch (anxiazhi){case "": resultnum = double.Parse(jiguo.Text);anxiazhi = s;break;case "+": resultnum = resultnum + double.Parse(jiguo.Text);anxiazhi = s;break;case "-": resultnum = resultnum - double.Parse(jiguo.Text);anxiazhi = s;break;case "*": resultnum = resultnum * double.Parse(jiguo.Text);anxiazhi = s;break;case "/": if (double.Parse(jiguo.Text) != 0.0){resultnum = resultnum / double.Parse(jiguo.Text);}else{resultnum = 0.0;}anxiazhi = s;break;default: break;}}else{anxiazhi = s;}}private void Button_Click_1(object sender, RoutedEventArgs e){if (anxiazhi == "="){gongshi.Text = "";jiguo.Text = "";anxiazhi = "";resultnum = 0.0;}string s = ((Button)sender).Content.ToString();jiguo.Text = jiguo.Text + s;gongshi.Text = gongshi.Text + s;}//按運算符號的事件處理private void fuhao_Click_1(object sender, RoutedEventArgs e){if (anxiazhi == "="){gongshi.Text = jiguo.Text;anxiazhi = "";}string s = ((Button)sender).Content.ToString();//獲得按鈕文本內(nèi)容gongshi.Text = gongshi.Text + s;OperationNum(s);jiguo.Text = "";}//按“=”號計算結果private void result_Click_1(object sender, RoutedEventArgs e){OperationNum("=");jiguo.Text = resultnum.ToString();}//清除操作private void del_Click_1(object sender, RoutedEventArgs e){jiguo.Text = "";gongshi.Text = "";anxiazhi = "";resultnum = 0.0;}//退格private void tuige_Click_1(object sender, RoutedEventArgs e){//獲取字符串長度int le = jiguo.Text.Length;int le2 = gongshi.Text.Length;if (le > 1&&le2>1){jiguo.Text = jiguo.Text.Substring(0, le - 1);gongshi.Text = gongshi.Text.Substring(0, le2 - 1);}}} }

?

轉(zhuǎn)載于:https://www.cnblogs.com/fengshen/p/3278602.html

總結

以上是生活随笔為你收集整理的一个用WPF做的简单计算器源代码的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。