Coding question

S

Sheldon

Hello -

I found the following code on the Internet which is perfect for my needs
with datetimepicker controls:

Private Sub dtpOther_ValueChanged(ByVal sender As Object, ByVal e As
System.EventArgs) Handles dtpOther.ValueChanged
Dim newTime As DateTime = dtpOther.Value
Dim hr As Integer = newTime.Hour
Dim min As Integer = newTime.Minute
Dim sec As Integer = newTime.Second

If (oldTime.Hour = 23 AndAlso newTime.Hour = 0) OrElse (oldTime.Hour
= 0 AndAlso newTime.Hour = 23) Then
hr = oldTime.Hour
End If
If (oldTime.Minute = 59 AndAlso newTime.Minute = 0) OrElse
(oldTime.Minute = 0 AndAlso newTime.Minute = 59) Then
min = oldTime.Minute
End If
If (oldTime.Second = 59 AndAlso newTime.Second = 0) OrElse
(oldTime.Second = 0 AndAlso newTime.Second = 59) Then
sec = oldTime.Second
End If
If hr <> newTime.Hour OrElse min <> newTime.Minute OrElse sec <>
newTime.Second Then
dtpOther.Value = DateTime.Now.Date.Add(New TimeSpan(hr, min, sec))
End If
oldTime = dtpOther.Value
End Sub

I have nine timepicker controls which all need to do the same thing as the
above. How can I consolidate it in code instead of doing one each of the Sub
for each of the controls?

I know it's an elementary question for a lot of you, but I'm just learning
this stuff.

Any help will be appreciated!
 
L

Lloyd Sheen

Sheldon said:
Hello -

I found the following code on the Internet which is perfect for my needs
with datetimepicker controls:

Private Sub dtpOther_ValueChanged(ByVal sender As Object, ByVal e As
System.EventArgs) Handles dtpOther.ValueChanged
Dim newTime As DateTime = dtpOther.Value
Dim hr As Integer = newTime.Hour
Dim min As Integer = newTime.Minute
Dim sec As Integer = newTime.Second

If (oldTime.Hour = 23 AndAlso newTime.Hour = 0) OrElse
(oldTime.Hour
= 0 AndAlso newTime.Hour = 23) Then
hr = oldTime.Hour
End If
If (oldTime.Minute = 59 AndAlso newTime.Minute = 0) OrElse
(oldTime.Minute = 0 AndAlso newTime.Minute = 59) Then
min = oldTime.Minute
End If
If (oldTime.Second = 59 AndAlso newTime.Second = 0) OrElse
(oldTime.Second = 0 AndAlso newTime.Second = 59) Then
sec = oldTime.Second
End If
If hr <> newTime.Hour OrElse min <> newTime.Minute OrElse sec <>
newTime.Second Then
dtpOther.Value = DateTime.Now.Date.Add(New TimeSpan(hr, min,
sec))
End If
oldTime = dtpOther.Value
End Sub

I have nine timepicker controls which all need to do the same thing as the
above. How can I consolidate it in code instead of doing one each of the
Sub
for each of the controls?

I know it's an elementary question for a lot of you, but I'm just learning
this stuff.

Any help will be appreciated!

There are two different ways to do it.

First is using AddHandler. This could be put in the load of the form as
follows:

AddHandler ControlName.ValueChanged , AddressOf dtpOther_ValueChanged

The second is to add each Control.ValueChanged in the Handles clause as
follows:

Private Sub dtpOther_ValueChanged(ByVal sender As Object, ByVal e As
System.EventArgs) Handles dtpOther.ValueChanged , OtherControl.ValueChanged
, AnotherControl.ValueChanged ....etc

Hope this helps
LS
 
J

Jack Jackson

There are two different ways to do it.

First is using AddHandler. This could be put in the load of the form as
follows:

AddHandler ControlName.ValueChanged , AddressOf dtpOther_ValueChanged

The second is to add each Control.ValueChanged in the Handles clause as
follows:

Private Sub dtpOther_ValueChanged(ByVal sender As Object, ByVal e As
System.EventArgs) Handles dtpOther.ValueChanged , OtherControl.ValueChanged
, AnotherControl.ValueChanged ....etc

Hope this helps
LS

And don't forget to change the method to use the sender parameter as
the object to modify.

Add:

Dim dtp As DateTimePicker = DirectCast(sender, DateTimePicker)

and change all references to dtpOther to dtp.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top