选择了DateTimePicker日期

时间:2013-01-03 15:46:32

标签: vb.net datetimepicker

我的Visual Basic项目遇到了一些问题。

如果在美国推出任何新电视剧/可供下载(第二天),我会制作一个节目。

(美国和Torrent是瑞典语的日子)

enter image description here

所以我已经这样做但我想添加一个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

所以,当标签说"星期一"时,如果我选择星期一,它会这样做。它将显示星期一可用的系列。嗯,这不起作用,我无法弄清楚原因。

任何人都可以向我解释我如何才能使这个工作,我做错了什么?

2 个答案:

答案 0 :(得分:1)

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

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

并检查用户是否在答案中已经提及的ValueChanged DateTimePicker事件中选择了一个日期。

答案 1 :(得分:0)

这是因为您没有告诉任何事情要听取datepicker1label4的更改。我会这样做,以便当datepicker发生变化(每次)时,内部代码将会触发,从而改变内容。您需要将ContentChanger子中的一些变量公开或在本地设置它们,否则它们将无法访问。

尝试以下内容:

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