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

VB form load event based on date

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

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

我原来是放在Form 2的Form Load Event里面的,没有显示msgbox,也没有显示button。

我从我的项目中剪切代码并将其粘贴到新项目的表单加载事件中以自行测试...显示消息框并显示按钮!! :)

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

再一次,没有消息框,没有按钮

我错过了什么?

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

首先,通读 DateTime 结构的文档。你可以在不使用字符串的情况下做你想做的一切。 DateTime 结构有一个 DayOfWeek 属性,以及 MonthDay 属性可以帮助你。

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

所以,考虑到所有这些(并假设您想按照代码建议在一年中的最后一个 工作日 显示按钮,而不仅仅是最后一个 day 在你的问题陈述的那一年),尝试这样的事情:

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