Home > Software engineering >  How to display error message when user pick more than 1 day then click the checkbox for halfday?
How to display error message when user pick more than 1 day then click the checkbox for halfday?

Time:06-03

I have two datepicker which are datepickerFrom and datepickerto. I want to show an error message when user pick more than 1 day and check the half day checkbox then click button submit it will show and error message.

For example : A pick 1/6/2022 - 3/6/2022, then A click check box for half day. When A clicked the submit button it'll show an error message like 'Sorry, You can't pick more than 1 date for half day!'

Here is my code for my checking error

Private Function ErrorFree() As Boolean 

If datepickerFrom.Date > datepickerto.Date Then 
   If chkHalfDay.Checked = True Then 
       DisplayMessage("error", "ERROR", "Sorry, You can't pick more than 1 date for half day!")
   Return False
   Exit Function
   End If
End If
Return True
End Function

CodePudding user response:

I am finally got the answer! so here is the code. then i call the function in datepickerFrom_DateChanged and datepickerto_DateChanged.

    Private Function CalculateDaysBetweenDates() As Integer
        Dim dateFrom As DateTime = Convert.ToDateTime(datepickerFrom.Date)
        Dim dateTo As DateTime = Convert.ToDateTime(datepickerto.Date)
        Dim ts As TimeSpan = dateTo.Subtract(dateFrom)
        If Convert.ToInt32(ts.Days) > 0 Then

            radHalfday.Enabled = False
        Else
            radHalfday.Enabled = True
        End If
        Return 1
    End Function
  • Related