计算具有日期值的文本框? [英] calculation on textboxes that have date value?

查看:84
本文介绍了计算具有日期值的文本框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个名为`borrower_date_txt,period_txt和ret_date_txt`的文本框,知道我想对这些文本框进行计算,我想在borrower_date_txt中输入日期(不仅是今天的日期),而在period_txt中我输入一个句点ret_date_txt中的示例5我想显示`borrower_date_txt + 5`,即在'ret_date_txt`中'borrower_date_txt'日期值的5天之后给我一个日期?

I have three textboxes named `borrower_date_txt,period_txt and ret_date_txt`, know i want to make calculation on those textboxes, I want to enter the date into the borrower_date_txt (not only today's date) and in the period_txt i enter a period for example 5 in the ret_date_txt i want to display the `borrower_date_txt + 5`, i.e give me the date after 5 days of 'borrower_date_txt' date value in the `ret_date_txt`?

推荐答案

因此,首先将文本框内容转换为相关类型:

So, start by converting the textbox contents to their relevant types:
DateTime borrowedOn;
if (!DateTime.TryParse(borrower_date_txt.Text, out borrowedOn))
    {
    // Report problem to user
    return;
    }
int loanPeriod;
if (!int.TryParse(period_txt.Text, out loanPeriod))
    {
    // Report problem to user
    return;
    }



然后,只需将一个添加到另一个,然后输出。


Then, just add the one to the other, and output it.

ret_date_txt.Text = borrowedOn.AddDays(loanPeriod).ToShortDateString();


尝试自己编写代码。

假设你想要单击按钮时计算return_date,因此您必须在按钮单击事件中编写代码。让我们来看看事件处理过程:

步骤1:如果使用为borrow_date和/或句点输入错误的值类型怎么办?可能,对。在客户端,您可以使用javascript进行验证。在代码隐藏中,您可以使用适当的TryParse方法来实现此目的。看看这个: C#TryParse [ ^ ]。

步骤2:如果borrow_date(DateTime)和句点(Int32)都通过了TryParse测试,则继续执行步骤3中止。

步骤3:使用适当的DateTime方法将期间添加到borrow_date以获取return_date,请查看: C#DateTime示例 [ ^ ]

步骤4:最后将return_date传递给相应的文本框。

结束
Try writing the code yourself.
Assuming you want to calculate the return_date upon button click, so you have to write your code in the button click event. Let walk through the event handling process:
Step 1: What if the use enter wrong value type for the borrow_date and/or the period? Possible, right. At the client side, you can javascript to do the validation. At the code-behind, you can use the appropriate TryParse methods to achieve this. Check this out: C# TryParse[^].
Step 2: if both borrow_date (DateTime) and period (Int32) pass the TryParse test, then proceed to step 3 else abort.
Step 3: Use the appropriate DateTime method to add the period to the borrow_date to get the return_date, check this out: C# DateTime Examples[^]
Step 4: Lastly pass the return_date to the appropriate text box.
The End


这篇关于计算具有日期值的文本框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆