David
David

Reputation: 621

DateTimePicker date selected

I'm having some problems with my Visual Basic Project.

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)

enter image description here

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:

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:

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?

Upvotes: 3

Views: 15254

Answers (2)

Olivier Jacot-Descombes
Olivier Jacot-Descombes

Reputation: 112762

You should base the comparison on the DayOfWeek enumeration instead of a string

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

and also check if the user selected a date in the ValueChanged event of the DateTimePicker as Chris already mentioned in his answer.

Upvotes: 1

Chris
Chris

Reputation: 5615

It is because you have not told anything to listen for a change in the datepicker1 or the label4. I would make it so that when the datepicker has changed (every time) the code inside will fire, changing the content. You will need to make some of the variables in the ContentChanger sub either public or set them locally else they will not be accessible.

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

Upvotes: 0

Related Questions