簡體   English   中英

在vb.net中的掩碼文本框中,將空的Day,Month或Year替換為Current Day,Month或Year。

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM