WPF通过出生日期计算年龄

在WPF 中用DatePicker控件输入出生年月,按回车键自动计算年龄,代码如下

UI界面

<Window x:Class="WPF通过出生日期计算年龄.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:WPF通过出生日期计算年龄"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <DatePicker Name="dteBirthday" HorizontalAlignment="Left" Margin="163,102,0,0" VerticalAlignment="Top" Height="26" Width="166" PreviewKeyDown="DteBirthday_PreviewKeyDown"/>
        <TextBox Name="tbAge" HorizontalAlignment="Left" Height="23" Margin="163,158,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="76"/>
        <ComboBox Name="cbAge" HorizontalAlignment="Left" Margin="260,158,0,0" VerticalAlignment="Top" Width="69">
            <ComboBoxItem Content="岁"/>
            <ComboBoxItem Content="月"/>
            <ComboBoxItem Content="日"/>
        </ComboBox>
        <Label Content="出生日期:" HorizontalAlignment="Left" Margin="81,101,0,0" VerticalAlignment="Top"/>
        <Label Content="年      龄:" HorizontalAlignment="Left" Margin="81,156,0,0" VerticalAlignment="Top"/>

    </Grid>
</Window>

后台代码:

注意,DatePicker键盘事件要用PreviewKeyDown而不用KeyDown事件

using System;
using System.Windows;
using System.Windows.Input;

namespace WPF通过出生日期计算年龄
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void DteBirthday_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                try
                {
                    //dteBirthday为空时跳出
                    DateTime dateTime = (DateTime)dteBirthday.SelectedDate.Value;
                }
                catch (Exception)
                {

                    return;
                }


                DateTime now = DateTime.Now;//当前时间用于对比

                if (dteBirthday.SelectedDate > DateTime.Now)
                {
                    MessageBox.Show("出生日期不能大于当前时间");
                    return;
                }

                int age = now.Year - dteBirthday.SelectedDate.Value.Year;//计算年龄
                if (age > 1)
                {
                    if ((now.Month > dteBirthday.SelectedDate.Value.Month) || (now.Month == dteBirthday.SelectedDate.Value.Month && now.Day >= dteBirthday.SelectedDate.Value.Day))
                    {
                        tbAge.Text = age.ToString();
                        cbAge.SelectedIndex = 0;
                    }
                    else
                    {
                        age--;
                        tbAge.Text = age.ToString();
                        cbAge.SelectedIndex = 0;
                    }

                }
                else if (age == 1)
                {
                    if ((now.Month > dteBirthday.SelectedDate.Value.Month) || (now.Month == dteBirthday.SelectedDate.Value.Month && now.Day >= dteBirthday.SelectedDate.Value.Day))
                    {
                        tbAge.Text = "1";
                        cbAge.SelectedIndex = 0;
                    }
                    else if (now.Month == dteBirthday.SelectedDate.Value.Month && now.Day < dteBirthday.SelectedDate.Value.Day)
                    {
                        tbAge.Text = "11";
                        cbAge.SelectedIndex = 1;
                    }
                    else
                    {
                        age = now.Month + 12 - dteBirthday.SelectedDate.Value.Month;
                        tbAge.Text = age.ToString();
                        cbAge.SelectedIndex = 1;
                    }
                }
                else
                {
                    if (now.Month > dteBirthday.SelectedDate.Value.Month)
                    {
                        age = now.Month - dteBirthday.SelectedDate.Value.Month;
                        tbAge.Text = age.ToString();
                        cbAge.SelectedIndex = 1;
                    }
                    else if ((now.Month == dteBirthday.SelectedDate.Value.Month) && (now.Day > dteBirthday.SelectedDate.Value.Day))
                    {
                        age = now.Day - dteBirthday.SelectedDate.Value.Day;
                        tbAge.Text = age.ToString();
                        cbAge.SelectedIndex = 2;
                    }
                    else
                    {
                        tbAge.Text = "1";
                        cbAge.SelectedIndex = 2;
                    }
                }
            }
        }
    }
}

效果图

思路其实很简单,先画一个树状图理清思路

 

在将共用部分合并

项目完整源码地址

https://gitee.com/xiaoniushengdaniu/wpf_birth_date_calculates_age

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值