WPF通过出生日期计算年龄

2023-10-30 11:33
文章标签 计算 wpf 年龄 出生日期

本文主要是介绍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

这篇关于WPF通过出生日期计算年龄的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/307636

相关文章

Python并行处理实战之如何使用ProcessPoolExecutor加速计算

《Python并行处理实战之如何使用ProcessPoolExecutor加速计算》Python提供了多种并行处理的方式,其中concurrent.futures模块的ProcessPoolExecu... 目录简介完整代码示例代码解释1. 导入必要的模块2. 定义处理函数3. 主函数4. 生成数字列表5.

使用WPF实现窗口抖动动画效果

《使用WPF实现窗口抖动动画效果》在用户界面设计中,适当的动画反馈可以提升用户体验,尤其是在错误提示、操作失败等场景下,窗口抖动作为一种常见且直观的视觉反馈方式,常用于提醒用户注意当前状态,本文将详细... 目录前言实现思路概述核心代码实现1、 获取目标窗口2、初始化基础位置值3、创建抖动动画4、动画完成后

Java计算经纬度距离的示例代码

《Java计算经纬度距离的示例代码》在Java中计算两个经纬度之间的距离,可以使用多种方法(代码示例均返回米为单位),文中整理了常用的5种方法,感兴趣的小伙伴可以了解一下... 目录1. Haversine公式(中等精度,推荐通用场景)2. 球面余弦定理(简单但精度较低)3. Vincenty公式(高精度,

windows和Linux使用命令行计算文件的MD5值

《windows和Linux使用命令行计算文件的MD5值》在Windows和Linux系统中,您可以使用命令行(终端或命令提示符)来计算文件的MD5值,文章介绍了在Windows和Linux/macO... 目录在Windows上:在linux或MACOS上:总结在Windows上:可以使用certuti

Python如何计算两个不同类型列表的相似度

《Python如何计算两个不同类型列表的相似度》在编程中,经常需要比较两个列表的相似度,尤其是当这两个列表包含不同类型的元素时,下面小编就来讲讲如何使用Python计算两个不同类型列表的相似度吧... 目录摘要引言数字类型相似度欧几里得距离曼哈顿距离字符串类型相似度Levenshtein距离Jaccard相

使用C#代码计算数学表达式实例

《使用C#代码计算数学表达式实例》这段文字主要讲述了如何使用C#语言来计算数学表达式,该程序通过使用Dictionary保存变量,定义了运算符优先级,并实现了EvaluateExpression方法来... 目录C#代码计算数学表达式该方法很长,因此我将分段描述下面的代码片段显示了下一步以下代码显示该方法如

最好用的WPF加载动画功能

《最好用的WPF加载动画功能》当开发应用程序时,提供良好的用户体验(UX)是至关重要的,加载动画作为一种有效的沟通工具,它不仅能告知用户系统正在工作,还能够通过视觉上的吸引力来增强整体用户体验,本文给... 目录前言需求分析高级用法综合案例总结最后前言当开发应用程序时,提供良好的用户体验(UX)是至关重要

如何用Java结合经纬度位置计算目标点的日出日落时间详解

《如何用Java结合经纬度位置计算目标点的日出日落时间详解》这篇文章主详细讲解了如何基于目标点的经纬度计算日出日落时间,提供了在线API和Java库两种计算方法,并通过实际案例展示了其应用,需要的朋友... 目录前言一、应用示例1、天安门升旗时间2、湖南省日出日落信息二、Java日出日落计算1、在线API2

poj 1113 凸包+简单几何计算

题意: 给N个平面上的点,现在要在离点外L米处建城墙,使得城墙把所有点都包含进去且城墙的长度最短。 解析: 韬哥出的某次训练赛上A出的第一道计算几何,算是大水题吧。 用convexhull算法把凸包求出来,然后加加减减就A了。 计算见下图: 好久没玩画图了啊好开心。 代码: #include <iostream>#include <cstdio>#inclu

uva 1342 欧拉定理(计算几何模板)

题意: 给几个点,把这几个点用直线连起来,求这些直线把平面分成了几个。 解析: 欧拉定理: 顶点数 + 面数 - 边数= 2。 代码: #include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstring>#include <cmath>#inc