VB表单基于日期的加载事件

时间:2015-01-01 02:23:54

标签: vb.net

我正在尝试根据日期显示vb中的辅助表单上的按钮(尝试仅在一年的最后一天显示重置按钮)。

我用以下代码尝试了一些不同的东西......

我原来把它放在Form 2的Form Load Event中,没有显示msgbox,按钮没有显示。

我将代码从我的项目中删除并粘贴到新项目的表单加载事件中以自行测试...显示Msgbox并显示按钮! :)

这让我想到也许我必须将代码放入主表单的表单加载事件中。我把它粘贴在那里并进行了修改以指向form2(当前版本的代码)....

再一次,没有msgbox,没有按钮

我错过了什么?

Private Sub Main_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    Dim date1 As String = String.Format("{0:MM/dd/yyyy}", DateTime.Now)
    Dim todaysdate As String = Format(Now, "Long Date")
    Dim dayofweek = todaysdate.Substring(0, todaysdate.IndexOf(","))
    Dim year As String = Now.Year
    Dim datecheck As String = "12/29/"
    Dim datecheck1 As String = "12/30/"
    Dim datecheck2 As String = "12/31/"

    ' Add Current Year to Date to Check variables
    datecheck = datecheck + year
    datecheck1 = datecheck1 + year
    datecheck2 = datecheck2 + year


    Dim expenddt As Date = Date.ParseExact(date1, date1, System.Globalization.DateTimeFormatInfo.InvariantInfo)

    Dim expenddt1 As Date = Date.ParseExact(datecheck, datecheck,
    System.Globalization.DateTimeFormatInfo.InvariantInfo)

    Dim expenddt2 As Date = Date.ParseExact(datecheck1, datecheck1,
    System.Globalization.DateTimeFormatInfo.InvariantInfo)

    Dim expenddt3 As Date = Date.ParseExact(datecheck2, datecheck2,
    System.Globalization.DateTimeFormatInfo.InvariantInfo)


    ' If DEC 29 or 30 Falls Fiday, Display Reset Button
    If date1 = datecheck And dayofweek = "Friday" Then
        ' MsgBox Used Only For Testing
        MsgBox("THIS ONE WORKED!")
        Form2.Reset.Visible = True
    End If

    If date1 = datecheck1 And dayofweek = "Friday" Then
        ' MsgBox Used Only For Testing
        MsgBox("THIS ONE WORKED!!")
        Form2.Reset.Visible = True
    End If


    ' If it's Dec 31 and it's Not Saturday or Sunday, Display Reset Button
    If date1 = datecheck2 and dayofweek <> "Saturday" and dayofweek <> "Sunday" Then
        ' MsgBox Used Only For Testing
        MsgBox("THIS ONE WORKED!!!")
        Form2.Reset.Visible = True
    End If

End Sub

1 个答案:

答案 0 :(得分:0)

首先,请阅读DateTime结构的文档。您可以在不使用字符串的情况下完成您尝试执行的所有操作。 DateTime结构具有DayOfWeek属性,MonthDay属性可以帮助您。

其次,使用ParseExact方法的方式是错误的(并不是说你应该最终使用它)。 ParseExact方法的第二个参数是您希望日期所在的格式字符串(类似“MM / dd / yyyy”)。传递格式化的日期是行不通的,从我的实验中,只会返回当前日期而不进行任何解析。

所以,考虑到所有这一切(并假设您希望在代码建议的一年中显示最后工作日的按钮,而不仅仅是最后在你提出的问题的一年中),尝试这样的事情:

Private Sub Main_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    Form2.Reset.Visible = ShouldShowResetButton(DateTime.Now)
End Sub


Private Function ShouldShowResetButton(currentDate As DateTime) As Boolean
    Return GetLastWeekdayInYear(currentDate.Year) = currentDate.Date
End Function


Private Function GetLastWeekdayInYear(year As Integer) As Date
    Dim lastDayInYear As Date


    lastDayInYear = New Date(year, 12, 31)

    Select Case lastDayInYear.DayOfWeek
        Case DayOfWeek.Sunday
            Return New Date(year, 12, 29)

        Case DayOfWeek.Saturday
            Return New Date(year, 12, 30)

        Case Else
            Return lastDayInYear

    End Select
End Function