473,385 Members | 1,798 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Coding question

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!
--
Sheldon
Jul 19 '08 #1
6 1105

"Sheldon" <Sh*****@discussions.microsoft.comwrote in message
news:7D**********************************@microsof t.com...
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!
--
Sheldon
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

Jul 19 '08 #2
Well, you got half the answer here. The rest of the answer is that you
have to determine which control was hit to cause the routine to run.

It gets messy! Losing control arrays is a real pain!

Mike

Sub AlarmChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) _
Handles Alarm1.ValueChanged, Alarm2.ValueChanged, Alarm3.ValueChanged,
Alarm4.ValueChanged, Alarm5.ValueChanged, _
Alarm6.ValueChanged, Alarm7.ValueChanged, Alarm8.ValueChanged,
Alarm9.ValueChanged, Alarm10.ValueChanged, _
Alarm11.ValueChanged, Alarm12.ValueChanged, Alarm13.ValueChanged,
Alarm14.ValueChanged, Alarm15.ValueChanged, _
Alarm16.ValueChanged, Alarm17.ValueChanged, Alarm18.ValueChanged,
Alarm19.ValueChanged, Alarm20.ValueChanged, _
Alarm21.ValueChanged, Alarm22.ValueChanged, Alarm23.ValueChanged,
Alarm24.ValueChanged, Alarm25.ValueChanged, _
Alarm26.ValueChanged, Alarm27.ValueChanged, Alarm28.ValueChanged,
Alarm29.ValueChanged, Alarm30.ValueChanged, _
Alarm31.ValueChanged, Alarm32.ValueChanged, Alarm33.ValueChanged,
Alarm34.ValueChanged, Alarm35.ValueChanged, _
Alarm36.ValueChanged, Alarm37.ValueChanged, Alarm38.ValueChanged,
Alarm39.ValueChanged, Alarm30.ValueChanged, _
Alarm41.ValueChanged, Alarm42.ValueChanged, _
Alarm1.Click, Alarm2.Click, Alarm3.Click, Alarm4.Click, Alarm5.Click,
_
Alarm6.Click, Alarm7.Click, Alarm8.Click, Alarm9.Click, Alarm10.Click,
_
Alarm11.Click, Alarm12.Click, Alarm13.Click, Alarm14.Click,
Alarm15.Click, _
Alarm16.Click, Alarm17.Click, Alarm18.Click, Alarm19.Click,
Alarm20.Click, _
Alarm21.Click, Alarm22.Click, Alarm23.Click, Alarm24.Click,
Alarm25.Click, _
Alarm26.Click, Alarm27.Click, Alarm28.Click, Alarm29.Click,
Alarm30.Click, _
Alarm31.Click, Alarm32.Click, Alarm33.Click, Alarm34.Click,
Alarm35.Click, _
Alarm36.Click, Alarm37.Click, Alarm38.Click, Alarm39.Click,
Alarm30.Click, _
Alarm41.Click, Alarm42.Click

If sender.Equals(Alarm1) Then : CheckAlarmVsWarn(Warn1, Alarm1) : Exit
Sub
ElseIf sender.Equals(Alarm2) Then : CheckAlarmVsWarn(Warn2, Alarm2)
: Exit Sub
ElseIf sender.Equals(Alarm3) Then : CheckAlarmVsWarn(Warn3, Alarm3)
: Exit Sub
ElseIf sender.Equals(Alarm4) Then : CheckAlarmVsWarn(Warn4, Alarm4)
: Exit Sub
ElseIf sender.Equals(Alarm5) Then : CheckAlarmVsWarn(Warn5, Alarm5)
: Exit Sub
ElseIf sender.Equals(Alarm6) Then : CheckAlarmVsWarn(Warn6, Alarm6)
: Exit Sub
ElseIf sender.Equals(Alarm7) Then : CheckAlarmVsWarn(Warn7, Alarm7)
: Exit Sub
ElseIf sender.Equals(Alarm8) Then : CheckAlarmVsWarn(Warn8, Alarm8)
: Exit Sub
ElseIf sender.Equals(Alarm9) Then : CheckAlarmVsWarn(Warn9, Alarm9)
: Exit Sub
ElseIf sender.Equals(Alarm10) Then : CheckAlarmVsWarn(Warn10,
Alarm10) : Exit Sub
ElseIf sender.Equals(Alarm11) Then : CheckAlarmVsWarn(Warn11,
Alarm11) : Exit Sub
.....repeat until sick of the whole thing! This is NUTZ!!!!!
On Sat, 19 Jul 2008 18:18:04 -0400, in
microsoft.public.dotnet.languages.vb "Lloyd Sheen" <a@b.cwrote:
>
"Sheldon" <Sh*****@discussions.microsoft.comwrote in message
news:7D**********************************@microso ft.com...
>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!
--
Sheldon

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
Jul 19 '08 #3
On Sat, 19 Jul 2008 18:18:04 -0400, "Lloyd Sheen" <a@b.cwrote:
>
"Sheldon" <Sh*****@discussions.microsoft.comwrote in message
news:7D**********************************@microso ft.com...
>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!
--
Sheldon

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.
Jul 19 '08 #4
Ju********@home.net wrote:
Well, you got half the answer here. The rest of the answer is that
you have to determine which control was hit to cause the routine to
run.
sender *is* the control that was hit.

So
Dim TheAlarm As Alarm = DirectCast(sender, Alarm)

CheckAlarm(TheAlarm)...
Jul 19 '08 #5
Sheldon wrote:
>
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?
In addition to using a single handler for all instances, as noted in other
posts, if you wanted to get fancy, you could create a new control class which
inherits timepicker, and adds the code above. Then use that in all your forms.
It is not difficult to do.
Jul 20 '08 #6
Sheldon-

Almost the same answer as steve Gerard, however then with the extention::
"Use an usercontrol."

http://msdn.microsoft.com/en-us/libr...ercontrol.aspx

-Cor

"Sheldon" <Sh*****@discussions.microsoft.comschreef in bericht
news:7D**********************************@microsof t.com...
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!
--
Sheldon
Jul 20 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Eakin, W | last post by:
After recently coming back to php after working in asp for several years, I want to do things well, so I'm very concerned with learning good coding practices. Also I'm trying to seperate logic...
4
by: Larry | last post by:
Help Please http://www.angelfire.com/folk/ps197_brooklyn_ny I currently have a main page that has a list of about 50 names. I then have 50 pages that are linked to the main page The...
144
by: Natt Serrasalmus | last post by:
After years of operating without any coding standards whatsoever, the company that I recently started working for has decided that it might be a good idea to have some. I'm involved in this...
7
by: Ralph Lund | last post by:
Hi. I am starting a new project with C#. I am searching for "good" coding conventions. I know that there are some coding conventions from microsoft, (but they are very extensive and not clear)....
1
by: R Reyes | last post by:
Hello All, I'm always looking for ways to improve my code. Most of the time (whenever I'm working on a project) I write a bunch of functions. Then after the project is finished, I put all the...
17
by: M.Siler | last post by:
I'm trying to get my head around a conversation I had with a developer the other day. We were talking about Codesmith vs. Hand coding. He's position is Codesmith is for junior to mid level...
7
by: Robert Seacord | last post by:
The CERT/CC has just deployed a new web site dedicated to developing secure coding standards for the C programming language, C++, and eventually other programming language. We have already...
3
by: bh | last post by:
If I want to loop through the values in a listbox, and get either all items in the box, or only selected items, based on a boolean variable passed into a subroutine, which method would be more...
1
by: Jennifer Jazz | last post by:
My question is regarding the mapping of Class diagram to the C++ coding. There are 3 realtions in Class diagram 1) Assosication 2) Composition 3) Aggregation (Weak Composition). ...
9
by: mekalai82 | last post by:
i have information.php file that file contain following coding <?php echo phpinfo(); ?> while i calling the URL ("http://localhost/information.php"). i am getting the coding <?php echo...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.