![](/img/trans.png)
[英]vb.net Get first day of year/ last day of last month / first day of last month from textbox date
[英]Replace empty Day or Month or Year with Current Day or Month or Year in masked textbox in vb.net
我有一个蒙版的文本框。
当用户输入其输入并按Tab键时。 我想检查他是否输入了有效日期?
我可以通过IsDate
Function进行检查。
如果无效,那么日期的哪一部分无效?
我不明白这一点。 我的意思是我不知道该怎么办。
如果我的上述问题得到了回答,那么我想问用户他的日,月或年是无效的。 是否要用当前日期或当前月份或当前年份替换?
或者,即使用户将年份保留为空白,我也想用当前年份代替空白。 或与月份相同。
您在这里有几个不同的问题,还不清楚您真正想要的是什么,但是要回答有关获取当前日/月/年的问题,请查看DateTime.Now 。
DateTime.Now.Day gets you the current day
DateTime.Now.Month gets you the current month
DateTime.Now.Year gets you the current year
这是我的问题的答案。
Private Sub mskdTxtBox1_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles mskdTxtBox1.Validating
'Check If Day is entered.
If mskdTxtBox1.Text.Substring(0, 2).Trim <> "" Then
'Check if Month is entered.
If mskdTxtBox1.Text.Substring(3, 2).Trim <> "" Then
'Check if year is not entered.
If mskdTxtBox1.Text.Length = 6 Then
'Check if Date and month entered are valid.
If IsDate(mskdTxtBox1.Text & Now.Year) Then
mskdTxtBox1.Text = mskdTxtBox1.Text & Now.Year
End If
End If
Else
'Check if Date entered will be valid for the current date that we want to create?
If IsDate(mskdTxtBox1.Text.Substring(0, 2).Trim & "-" & Now.Month & "-" & Now.Year) Then
'Check if month is two digit?
If Now.Month.ToString.Length < 2 Then
mskdTxtBox1.Text = mskdTxtBox1.Text.Substring(0, 2).Trim & "-0" & Now.Month & "-" & Now.Year
Else
mskdTxtBox1.Text = mskdTxtBox1.Text.Substring(0, 2).Trim & "-" & Now.Month & "-" & Now.Year
End If
End If
End If
Else
Dim TodaysDay As String = Now.Day
Dim TodaysMonth As String = Now.Month
'Check if Current day is two digit?
If Now.Day < 10 Then
TodaysDay = "0" & Now.Day
End If
'Check if Current Month is two digit?
If Now.Month < 10 Then
TodaysMonth = "0" & Now.Month
End If
mskdTxtBox1.Text = TodaysDay & "-" & TodaysMonth & "-" & Now.Year
End If
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.