【问题标题】:Month Calendar vb.net Multiple Date Selection月历 vb.net 多个日期选择
【发布时间】:2015-09-11 20:18:18
【问题描述】:

我的最大多日期选择数是 7,所以我创建了 7 个文本框,以便将每个日期存储到相应的文本框中。 我知道如何获取开始日期和结束日期。日期之间我如何将它们存储在文本框中?

【问题讨论】:

    标签: vb.net date monthcalendar


    【解决方案1】:

    MonthCalendar 选择是范围,因此您可以使用使用 AddDays 函数的 Do 循环来确定开始日期和结束日期之间的日期。这是一个假设您有 7 个具有以下命名约定的文本框的示例: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
    

    【讨论】:

    • 这个函数将被插入到 Private Sub MonthCalendar1_DateSelected(sender As Object, e As DateRangeEventArgs) Handles MonthCalendar1.DateSelected ?我试过了,这个错误发生了 Object reference not set to an instance of an object.
    • 这应该是该代码的好地方。你有 txtDate1 - txtDate7 命名约定的文本框吗?
    • 是的,文本框的命名是 txtdate1 txtdate2 e.t.c
    猜你喜欢
    • 2010-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-30
    • 2019-02-13
    相关资源
    最近更新 更多