3

我的 Visual Basic 项目有一些问题。

我正在制作一个程序,它会显示美国是否有新的电视剧/可供下载(第二天)

(US 和 Torrent 是瑞典语的天数)

在此处输入图像描述

所以我已经这样做了,但我想添加一个 DateTimePicker,所以我可以选择一天,然后检查可用的内容。好吧,我注意到 DateTimePicker 总是从当前日期开始,所以我只是像这样添加它:

Dim DateTimePickerDay = Me.DateTimePicker1.Value.DayOfWeek.ToString()

Dim USstatus = "Tomorrow"
Dim DownloadStatus = "Today"

If DateTimePickerDay = "Monday" Then
  StatusLabel1.Text = USstatus
  StatusLabel1.ForeColor = Color.Blue
  StatusLabel5.Text = DownloadStatus
  StatusLabel5.ForeColor = Color.Green
  StatusLabel6.Text = DownloadStatus
  StatusLabel6.ForeColor = Color.Green
End If

但现在我希望能够使用 DateTimePicker 更改日期并查看当天可用的内容。因此,我尝试将 Lebel 更改为这样的一天:

Private Sub Button11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button11.Click
  Me.Label4.Text = Me.DateTimePicker1.Value.DayOfWeek.ToString()
End Sub

接着:

If Me.Label4.Text = "Monday" Then
  StatusLabel1.Text = USstatus
  StatusLabel1.ForeColor = Color.Blue
  StatusLabel5.Text = DownloadStatus
  StatusLabel5.ForeColor = Color.Green
  StatusLabel6.Text = DownloadStatus
  StatusLabel6.ForeColor = Color.Green
End If

因此,当标签显示“星期一”时,如果我选择星期一,它就会这样做。它将显示星期一有哪些系列可用。好吧,这不起作用,我不知道为什么。

任何人都可以为我解释如何让这个工作,我做错了什么?

4

2 回答 2

1

您应该基于DayOfWeek枚举而不是字符串进行比较

If Me.DateTimePicker1.Value.DayOfWeek = DayOfWeek.Monday Then
    ...
End If

ValueChanged如果DateTimePicker克里斯在他的回答中已经提到过,还检查用户是否选择了日期。

于 2013-01-03T19:18:09.667 回答
0

这是因为你没有告诉任何东西来听听datepicker1或 的变化label4。我会这样做,以便当datepicker(每次)发生变化时,里面的代码会触发,改变内容。您需要将ContentChangersub 中的一些变量设为 public 或在本地设置它们,否则它们将无法访问。

尝试以下操作:

Private Sub DateTimePicker1_ValueChanged(sender As Object, e As EventArgs) Handles DateTimePicker1.ValueChanged
    ' When the datepicker1 has changed (date has been chosen).. do something with that date
    ' Get the datepicker's chosen day name
    Dim dayOfWeek As String = DateTimePicker1.Value.DayOfWeek.ToString()

    ' Update the label to show the user
    Label4.Text = dayOfWeek

    ' Now change the content shown according to the date range
    ContentChanger(dayOfWeek)

End Sub


Sub ContentChanger(dayOfWeek As String)

    If dayOfWeek = "Monday" Then
        StatusLabel1.Text = USstatus
        StatusLabel1.ForeColor = Color.Blue
        StatusLabel5.Text = DownloadStatus
        StatusLabel5.ForeColor = Color.Green
        StatusLabel6.Text = DownloadStatus
        StatusLabel6.ForeColor = Color.Green
    End If

End Sub
于 2013-01-03T19:02:39.310 回答