WPF实现一个简单的多运算符表达式计算器

标题:WPF实现一个简单的多运算符表达式计算器

1.先看下效果图在这里插入图片描述
首先外围给了一个grid 把他分成了两行 第一行用来显示文本框给了一个低于第二行的高度 第二行用来存按钮 窗体的大小自己去调就好了 我这里给的是380x268

<Grid.RowDefinitions>
            <RowDefinition Height="0.7*"></RowDefinition>
            <RowDefinition></RowDefinition>
        </Grid.RowDefinitions>

这是一个非常简单的布局 没有用到样式 头部是用了一个Border给一个圆弧实现的 代码如下

<Border Margin="5" Padding="5" Background="White" BorderBrush="Black" BorderThickness="3,5,3,5" CornerRadius="10" VerticalAlignment="Top" Height="130" Width="240">
            <TextBlock Name="ShowNumText" Height="100" Width="auto" VerticalAlignment="Top" FontSize="50" HorizontalAlignment="Right" >
               
            </TextBlock>
        </Border>

接下来就是按键部分了 用了一个UniformGrid布局 类似于一个表格 给4行4列 最后再往里面添加按钮实现的 分别给每个按钮设置背景颜色,字体颜色以及单击事件(一共4类单击事件 分别是数字的、运算符的、等于号、还有一个清空C)

<UniformGrid Grid.Row="1" Rows="4" Columns="4" Height="200" Width="250">
            <Button Name="btn1" Content="1"  FontSize="35" Background="Black" Foreground="White" Click="btn1_Click_1"/>
            <Button Name="btn2" Content="2"  FontSize="35" Background="Black" Foreground="White" Click="btn1_Click_1"/>
            <Button Name="btn3" Content="3"  FontSize="35" Background="Black" Foreground="White" Click="btn1_Click_1"/>
            <Button Name="btnD" Content="÷"  FontSize="35" Background="Black" Foreground="White" Click="btnD_Click"/>
            <Button Name="btn4" Content="4"  FontSize="35" Background="Black" Foreground="White" Click="btn1_Click_1"/>
            <Button Name="btn5" Content="5"  FontSize="35" Background="Black" Foreground="White" Click="btn1_Click_1"/>
            <Button Name="btn6" Content="6"  FontSize="35" Background="Black" Foreground="White" Click="btn1_Click_1"/>
            <Button Name="btnX" Content="X"  FontSize="35" Background="Black" Foreground="White" Click="btnD_Click"/>
            <Button Name="btn7" Content="7"  FontSize="35" Background="Black" Foreground="White" Click="btn1_Click_1"/>
            <Button Name="btn8" Content="8"  FontSize="35" Background="Black" Foreground="White" Click="btn1_Click_1"/>
            <Button Name="btn9" Content="9"  FontSize="35" Background="Black" Foreground="White" Click="btn1_Click_1"/>
            <Button Name="btnM" Content="-" FontSize="35" Background="Black" Foreground="White" Click="btnD_Click"/>
            <Button Name="btnC" Content="C"  FontSize="35" Background="Black" Foreground="White" Click="btnC_Click"/>
            <Button Name="btn0" Content="0"  FontSize="35" Background="Black" Foreground="White" Click="btn1_Click_1"/>
            <Button Name="btnE" Content="+"  FontSize="35" Background="Black" Foreground="White" Click="btnD_Click"/>
            <Button Name="btnP" Content="="  FontSize="35" Background="Black" Foreground="White" Click="btnP_Click"/>
        </UniformGrid>

这样我们的xaml样式就写完了,接下来就是后端了

上面的代码我们看到我们已经定义了单击事件 首先找到数字的单击事件写上以下代码

var v = sender as Button;
            switch (v.Content.ToString())
            {
                case "1":
                    ShowNumText.Text += 1;
                    break;
                case "2":
                    ShowNumText.Text += 2;
                    break;
                case "3":
                    ShowNumText.Text += 3;
                    break;
                case "4":
                    ShowNumText.Text += 4;
                    break;
                case "5":
                    ShowNumText.Text += 5;
                    break;
                case "6":
                    ShowNumText.Text += 6;
                    break;
                case "7":
                    ShowNumText.Text += 7;
                    break;
                case "8":
                    ShowNumText.Text += 8;
                    break;
                case "9":
                    ShowNumText.Text += 9;
                    break;
                case "0":
                    ShowNumText.Text += 0;
                    break;

意思就是判断一下用户点击的是哪一个数字 然后把他加到文本框内

接下来就是运算符的单击事件 同理数字的

 if (ShowNumText.Text == "")
                return;
            var v1 = sender as Button;
            switch (v1.Content.ToString())
            {
                case "+":
                    ShowNumText.Text += "+";
                    break;
                case "-":
                    ShowNumText.Text += "-";
                    break;
                case "X":
                    ShowNumText.Text += "X";
                    break;
                case "÷":
                    ShowNumText.Text += "÷";
                    break;
            }   

然后导入命名空间
using System.Data;
这个命名空间里面有一个超级好用的方法Compute
Compute的意思简单来说就是放入一个string类型的带有表达式的字符串计算,
找到等于号的事件 加入代码

try
            {
                string str= ShowNumText.Text.Replace('X', '*');
                str= str.Replace('÷', '/');
                DataTable dt = new DataTable();
                string v = dt.Compute(str, null).ToString();
                ShowNumText.Text = v.ToString();
            }
            catch { ShowNumText.Text = ""; }

用Replace方法过滤掉 x和÷
因为Compute 是不接收数学的乘和除的

最后在清空事件里加入一个ShowNumText.Text = "";//清空文本框
这样我们的计算器就写完了!!!

前台xaml

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication1"
        mc:Ignorable="d"
        Title="计算机" Height="380" Width="268">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="0.7*"></RowDefinition>
            <RowDefinition></RowDefinition>
        </Grid.RowDefinitions>
        <Border Margin="5" Padding="5" Background="White" BorderBrush="Black" BorderThickness="3,5,3,5" CornerRadius="10" VerticalAlignment="Top" Height="130" Width="240">
            <TextBlock Name="ShowNumText" Height="100" Width="auto" VerticalAlignment="Top" FontSize="50" HorizontalAlignment="Right" >
               
            </TextBlock>
        </Border>
        <UniformGrid Grid.Row="1" Rows="4" Columns="4" Height="200" Width="250">
            <Button Name="btn1" Content="1"  FontSize="35" Background="Black" Foreground="White" Click="btn1_Click_1"/>
            <Button Name="btn2" Content="2"  FontSize="35" Background="Black" Foreground="White" Click="btn1_Click_1"/>
            <Button Name="btn3" Content="3"  FontSize="35" Background="Black" Foreground="White" Click="btn1_Click_1"/>
            <Button Name="btnD" Content="÷"  FontSize="35" Background="Black" Foreground="White" Click="btnD_Click"/>
            <Button Name="btn4" Content="4"  FontSize="35" Background="Black" Foreground="White" Click="btn1_Click_1"/>
            <Button Name="btn5" Content="5"  FontSize="35" Background="Black" Foreground="White" Click="btn1_Click_1"/>
            <Button Name="btn6" Content="6"  FontSize="35" Background="Black" Foreground="White" Click="btn1_Click_1"/>
            <Button Name="btnX" Content="X"  FontSize="35" Background="Black" Foreground="White" Click="btnD_Click"/>
            <Button Name="btn7" Content="7"  FontSize="35" Background="Black" Foreground="White" Click="btn1_Click_1"/>
            <Button Name="btn8" Content="8"  FontSize="35" Background="Black" Foreground="White" Click="btn1_Click_1"/>
            <Button Name="btn9" Content="9"  FontSize="35" Background="Black" Foreground="White" Click="btn1_Click_1"/>
            <Button Name="btnM" Content="-" FontSize="35" Background="Black" Foreground="White" Click="btnD_Click"/>
            <Button Name="btnC" Content="C"  FontSize="35" Background="Black" Foreground="White" Click="btnC_Click"/>
            <Button Name="btn0" Content="0"  FontSize="35" Background="Black" Foreground="White" Click="btn1_Click_1"/>
            <Button Name="btnE" Content="+"  FontSize="35" Background="Black" Foreground="White" Click="btnD_Click"/>
            <Button Name="btnP" Content="="  FontSize="35" Background="Black" Foreground="White" Click="btnP_Click"/>
        </UniformGrid>
    </Grid>
</Window>

后台代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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;
using System.Data;
namespace WpfApplication1
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            
        }
        private void btn1_Click_1(object sender, RoutedEventArgs e)//0-9绑定同一个事件
        { 
            var v = sender as Button;
            switch (v.Content.ToString())
            {
                case "1":
                    ShowNumText.Text += 1;
                    break;
                case "2":
                    ShowNumText.Text += 2;
                    break;
                case "3":
                    ShowNumText.Text += 3;
                    break;
                case "4":
                    ShowNumText.Text += 4;
                    break;
                case "5":
                    ShowNumText.Text += 5;
                    break;
                case "6":
                    ShowNumText.Text += 6;
                    break;
                case "7":
                    ShowNumText.Text += 7;
                    break;
                case "8":
                    ShowNumText.Text += 8;
                    break;
                case "9":
                    ShowNumText.Text += 9;
                    break;
                case "0":
                    ShowNumText.Text += 0;
                    break;
            }  
        }
        private void btnD_Click(object sender, RoutedEventArgs e)//运算符也绑定同一个事件
        {
            if (ShowNumText.Text == "")
                return;
            var v1 = sender as Button;
            switch (v1.Content.ToString())
            {
                case "+":
                    ShowNumText.Text += "+";
                    break;
                case "-":
                    ShowNumText.Text += "-";
                    break;
                case "X":
                    ShowNumText.Text += "X";
                    break;
                case "÷":
                    ShowNumText.Text += "÷";
                    break;
            }   
        }
        private void btnP_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                string str= ShowNumText.Text.Replace('X', '*');
                str= str.Replace('÷', '/');
                DataTable dt = new DataTable();
                string v = dt.Compute(str, null).ToString();
                ShowNumText.Text = v.ToString();
            }
            catch { ShowNumText.Text = ""; }
        }
        private void btnC_Click(object sender, RoutedEventArgs e)
        {
            ShowNumText.Text = "";//清空文本框
        }
    }
}

感谢你的观看!!!

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单WPF登录界面的实现: 1. 创建一个新的WPF应用程序项目。 2. 在项目中添加一个新的Window,用作登录窗口。 3. 在登录窗口中添加两个TextBox控件,用于输入用户名和密码。可以设置控件的Name、Text等属性,以及控件的布局和样式。 4. 添加一个Button控件,用于触发登录操作。可以设置控件的Name、Content等属性,以及控件的布局和样式。 5. 在Button的Click事件中编写登录逻辑。可以先获取TextBox中的用户名和密码,然后在服务端进行验证。验证成功后可以显示一个提示框或跳转到其他页面。 以下是一个简单的代码示例,用于实现WPF登录界面: ```csharp <Window x:Class="WpfApp1.LoginWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="LoginWindow" Height="250" Width="350"> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="100" /> <ColumnDefinition Width="*" /> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> </Grid.RowDefinitions> <Label Grid.Row="0" Grid.Column="0" Content="Username:" VerticalAlignment="Center" Margin="10" /> <TextBox Grid.Row="0" Grid.Column="1" Name="txtUsername" Margin="10" /> <Label Grid.Row="1" Grid.Column="0" Content="Password:" VerticalAlignment="Center" Margin="10" /> <PasswordBox Grid.Row="1" Grid.Column="1" Name="txtPassword" Margin="10" /> <Button Grid.Row="2" Grid.Column="1" Name="btnLogin" Content="Login" Margin="10" Click="btnLogin_Click" /> </Grid> </Window> ``` ```csharp private void btnLogin_Click(object sender, RoutedEventArgs e) { string username = txtUsername.Text; string password = txtPassword.Password; // TODO: 连接服务端进行验证 // TODO: 登录成功后显示提示框或跳转到其他页面 } ``` 希望这个示例能够对你有所帮助。如果你有其他问题或需要进一步帮助,请随时提出。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值