2

我有一个使用 2 个日期时间选择器并在文本框中显示天数差异的子程序,我希望子程序告诉用户天数是 0 还是大于 10,允许的天数在 1 到 10 之间。然后让用户再次选择日期

Private Sub btnCalculate_Click(sender As System.Object, e As System.EventArgs)Handles btnCalculate.Click

 Dim dt1 As DateTime = Convert.ToDateTime(DateTimePicker1.Text)

            Dim dt2 As DateTime = Convert.ToDateTime(DateTimePicker2.Text)

            Dim ts As TimeSpan = dt2.Subtract(dt1)

            If Convert.ToInt32(ts.Days) >= 0 Then

                txtDays.Text = "Total Days are "

                txtDays.AppendText(Environment.NewLine & Convert.ToInt32(ts.Days))


            Else

                MessageBox.Show("Invalid Input for dates (Start Date Cannot exist before End date)")

            End If


        End If
End Sub
4

1 回答 1

2
If ts.Days >= 1 AndAlso ts.Days <= 10 Then
    ' OK  
    txtDays.AppendText(Environment.NewLine & "Total Days are: " & ts.Days)
Else
     ' out of range
     MessageBox.Show("Days allowed are between 1 to 10.")
End If
于 2013-09-18T09:17:50.960 回答