469,909 Members | 1,686 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,909 developers. It's quick & easy.

DateTimePicker questions

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
Nov 22 '05 #1
2 1967
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


"Angelina" <an*******@discussions.microsoft.com> wrote in message
news:06****************************@phx.gbl...
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

Nov 22 '05 #2
"IbrahimMalluf" <Ib*****@malluf.com> wrote...

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.

Nov 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Angelina | last post: by
reply views Thread by Sumit | last post: by
4 posts views Thread by Jacek Jurkowski | last post: by
7 posts views Thread by Clamara | last post: by
3 posts views Thread by Charlie | last post: by
2 posts views Thread by mafandon | last post: by
3 posts views Thread by Simon Tamman | last post: by
4 posts views Thread by jehugaleahsa | last post: by
1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.