taby
taby

Reputation: 21

Adding 7 days to a Friday?

I have to add 7 days to a Friday from a DateTimePicker. Here's my code.

I know that it creates a loop because 7 days from Friday is Friday. Then it repeats itself, how can I stop it from repeating after the first Friday?

        If Me.DateTimePicker1.Value.DayOfWeek = DayOfWeek.Saturday _
        Or Me.DateTimePicker1.Value.DayOfWeek = DayOfWeek.Sunday Then
            If Me.DateTimePicker1.Value.DayOfWeek = DayOfWeek.Saturday Then
                Me.DateTimePicker1.Value = Me.DateTimePicker1.Value.AddDays(-1)
            Else
                Me.DateTimePicker1.Value = Me.DateTimePicker1.Value.AddDays(-2)
            End If
        End If
        If Me.DateTimePicker1.Value.DayOfWeek = DayOfWeek.Friday Then
            Me.DateTimePicker1.Value = Me.DateTimePicker1.Value.AddDays(7)
        End If
    End Sub
End Class

Upvotes: 1

Views: 82

Answers (1)

Psi
Psi

Reputation: 6783

Create a global state variable:

Dim busy as Boolean
busy = False

In your sub, you do something like this:

Sub dateTimePicker1Change...
     if not busy then 

          busy = True  
          ' Your logic here
          busy = False
     End if
End Sub

So with this, your event should fire twice, but when it is fired the second time while you are still processing the first event, you have set your busy state to True which immediately leads to a return of the second event handling (with no further triggering).

Upvotes: 1

Related Questions