DateTimePicker questions

  • Thread starter Thread starter Angelina
  • Start date Start date
A

Angelina

Hi,

I have got two DateTimePicker's on my form to accept a
guest arrival date and departure date.what i want to
acheieve is ensure that the DateTimePicker2 (departure)
is always greater than DateTimePicker1 (arrival date).
can anyone help me on the code for this? as u can tell,
im new learner.

i also want the days between the arrival date and
deparure date to be displayed in a 'length of stay
textbox'. i.e 2 nights stay.
or i want it so if they select a arrival date and enter 3
nights stay in the 'length of stay textbox' to adjust the
departure date automatically. Is this possible. can any
one help me on this coding or point me in the right
direction for any useful reasources.

Thx
 
Hello Angelina

dtp1 and dpt in the code below are datetimepickers. Hope this helps

Ibrahim Malluf

Private Sub dtp2_ValueChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles dtp2.ValueChanged

Dim MyDays As Int32

Dim Date2 As DateTimePicker = DirectCast(sender, DateTimePicker)

MyDays = DateDiff(DateInterval.Day, dtp1.Value, Date2.Value)

If MyDays < 1 Then Date2.Value = DateAdd(DateInterval.Day, 1, Date2.Value)

Me.txtDays.Text = MyDays

End Sub

Private Sub dtp1_ValueChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles dtp1.ValueChanged

Dim MyDays As Int32

Dim Date1 As DateTimePicker = DirectCast(sender, DateTimePicker)

MyDays = DateDiff(DateInterval.Day, Date1.Value, dtp2.Value)

If MyDays < 1 Then

dtp2.Value = DateAdd(DateInterval.Day, 1, dtp2.Value)

MyDays = DateDiff(DateInterval.Day, Date1.Value, dtp2.Value)

End If

Me.txtDays.Text = MyDays

End Sub
 
Hello... I like to study code because I always learn something from it... I
have some comments and questions if you don't mind.

First:
Dim Date2 As DateTimePicker = DirectCast(sender, DateTimePicker)

That's cool but it's the only control not referenced by name. Is there a
reason you are willing to access Me.txtDays and Me.dtp1 but decided not to
access dtp2.Value directly in the dtp2 event handler (and dtp1.Value in the
dtp1 event handler?)

And MyDays should really be a Long I believe...
Dim MyDays As Long = DateDiff(DateInterval.Day, Me.dt1.Value, Me.dt2.Value)

To accomodate the length of the stay this line should add the number of
days in the textbox rather than 1.
If MyDays < 1 Then
dtp2.Value = DateAdd(DateInterval.Day, 1, dtp2.Value)

Strictly speaking the assignment ought to be through MyDays.ToString
Me.txtDays.Text = MyDays.ToString


And I added a handler for the textbox:

Private Sub txtDays_TextChanged(ByVal sender As Object, ByVal e As
System.EventArgs) Handles txtDays.TextChanged

Dim days As Double = Val(Me.txtDays.Text)

If (days < 0) Then
days = 0
Me.txtDays.Text = days.ToString
End If

Me.dtp2.Value = DateAdd(DateInterval.Day, Val(Me.txtDays.Text),
dtp1.Value)

End Sub


And my question... if you (or somebody knows)

What causes Windows to stop looping between events when all the
"ValueChanged" handlers are changing the values of other controls. I notice
the other event handlers are called by first event handler but it eventually
exits.
 
Back
Top