c# - 如何从此函数将当前年龄检索到文本框?

标签 c#

public static int CalculateAge(DateTime birthdate)
        {
             int years = DateTime.Now.Year - birthdate.Year;
             if (DateTime.Now.Month < birthdate.Month 
             || (DateTime.Now.Month == birthdate.Month 
             && DateTime.Now.Day < birthdate.Day))
             years--;
             return years;
        }

最佳答案

只需这样做:

textBox1.Text = Convert.ToString(CalculateAge(someDate));

您必须将计算值分配给文本框的 Text 属性。但是由于函数的返回类型是 int,您首先必须将其转换为 string,因为 TextBox 只能显示字符串。

关于c# - 如何从此函数将当前年龄检索到文本框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21376524/

相关文章:

c# - ASP MVC 在不查询数据库的情况下填充下拉列表的方法

c# - 如何在 .NET Core 3.0 中为 WPF 应用程序引用 System.Windows.Forms?

c# - C# Windows 应用程序中的密码散列,缺少 ASP.NET 的 FormsAuthentication?

c# - 在 C# .NET 中使用操作系统的文件类型镜像

c# - 如何将值关联到函数调用?

c# - 在 .NET Core 中使用端口调用端点时出现无效 URL 异常

c# - 如何使用 MongoDB C# 将新对象插入(推送)到嵌套数组中?

c# - Web Api Request.CreateResponse HttpResponseMessage 没有智能感知 VS2012

c# - 无法打开源 'blah' 的日志。您可能没有写入权限。嗯?

c# - 双工命名管道 - 单个管道上的最大客户端数量