vb.net 已选择 DateTimePicker 日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14142235/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
DateTimePicker date selected
提问by David
I'm having some problems with my Visual Basic Project.
我的 Visual Basic 项目有一些问题。
Im making a program which desplays if there come out any new TV series in the US/ Available for download (next day)
我正在制作一个节目,如果美国有任何新电视剧出现,它就会显示/可供下载(第二天)
(US and Torrent are days in Swedish)
(美国和 Torrent 是瑞典语中的天)


So I have done that but I want to add a DateTimePicker, so I can select a day and then check whats Available. Well I noticed that the DateTimePicker always starts at the current date so I just added it like so:
所以我已经这样做了,但我想添加一个 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
But now I want to be able to change the day using the DateTimePicker and see whats available for that day. So I tried to change a Lebel to the day like so:
但是现在我希望能够使用 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
and then:
进而:
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
So when the Label says "Monday", which it does if I choose Monday. It's going to show which series are available on Monday. Well this doesn't work and I can't figure out why.
因此,当标签显示“星期一”时,如果我选择星期一,它就会这样做。它将显示周一有哪些系列可用。好吧,这不起作用,我不知道为什么。
Can anyone explain for me how I can get this working and what do I do wrong?
任何人都可以为我解释如何让这个工作,我做错了什么?
回答by Olivier Jacot-Descombes
You should base the comparison on the DayOfWeekenumeration instead of a string
您应该基于DayOfWeek枚举而不是字符串进行比较
If Me.DateTimePicker1.Value.DayOfWeek = DayOfWeek.Monday Then
...
End If
and also check if the user selected a date in the ValueChangedevent of the DateTimePickeras Chris already mentioned in his answer.
并检查用户是否选择了一个日期,ValueChanged以防万一DateTimePicker克里斯在他的回答中已经提到。
回答by Chris
It is because you have not told anything to listen for a change in the datepicker1or the label4. I would make it so that when the datepickerhas changed (every time) the code inside will fire, changing the content. You will need to make some of the variables in the ContentChangersub either public or set them locally else they will not be accessible.
这是因为你没有告诉任何东西来监听datepicker1或 中的变化label4。我会这样做,以便当datepicker(每次)更改时,里面的代码将触发,更改内容。您需要将ContentChangersub中的一些变量设为 public 或在本地设置它们,否则它们将无法访问。
Try something like the following:
请尝试以下操作:
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

