我的最大多日期选择号是7,因此我创建了7个文本框,以便将每个日期存储到相应的文本框中。 我知道如何获得开始日期和结束日期。如何在日期之间将它们存储在文本框中?
答案 0 :(得分:0)
MonthCalendar选择是范围,因此您可以使用使用AddDays函数的Do循环来确定开始日期和结束日期之间的日期。这个例子假设你有7个TextBox,它们具有以下命名约定:txtDate1,txtDate2,txtDate3等。
'First Date
txtDate1.Text = MonthCalendar1.SelectionStart.ToShortDateString()
Dim intCursor As Integer = 1
'Keep adding days to the starting date until you've reached the end.
Do Until MonthCalendar1.SelectionStart.Date.AddDays((intCursor)) >= MonthCalendar1.SelectionEnd.Date
Dim dteTarget As Date = MonthCalendar1.SelectionStart.Date.AddDays((intCursor))
'Casts the Textbox using a naming convention: txtDate1, txtDate2, txtDate3, etc.
CType(Me.Controls("txtDate" & (intCursor + 1).ToString()), TextBox).Text = dteTarget.ToShortDateString()
intCursor += 1
Loop
'If the end date is not the same as the start date then add that.
If MonthCalendar1.SelectionEnd.Date <> MonthCalendar1.SelectionStart.Date Then
CType(Me.Controls("txtDate" & (intCursor + 1).ToString()), TextBox).Text = MonthCalendar1.SelectionEnd.Date.ToShortDateString()
End If