1 Event Handler for multiple DateTimePickers - Not working for me???

Good morning, I have a Windows Form (vb.net) and I am dynamically creating multiple DateTimePicker controls. The number of controls depends on how many employees are currently registered in the database. So for each employee they get a start and end time control for their schedule. I have read multiple articles and solutions and I am simply stuck. I need 1 handler to fire anytime a timepicker value is changed. Once the value is changed, the textbox next to it will show the timespan in hours that will be added to the schedule...

Any help would be great
Dim DateTimePicker As New DateTimePicker
                DateTimePicker.Name = "Start" & counter
                DateTimePicker.Size = New Size(80, 25)
                DateTimePicker.Location = New Point(Combo2.Location.X + 135, Combo2.Location.Y)
                DateTimePicker.ShowUpDown = True
                DateTimePicker.Value = "1/1/2018 06:00 AM"
                DateTimePicker.Format = DateTimePickerFormat.Custom
                DateTimePicker.CustomFormat = "hh:mm tt"

                Dim DateTimePick As New DateTimePicker
                DateTimePick.Name = "End" & counter
                DateTimePick.Size = New Size(80, 25)
                DateTimePick.Location = New Point(DateTimePicker.Location.X + 90, DateTimePicker.Location.Y)
                DateTimePick.ShowUpDown = True
                DateTimePick.Value = "1/1/2018 12:00 PM"
                DateTimePick.Format = DateTimePickerFormat.Custom
                DateTimePick.CustomFormat = "hh:mm tt"
AddHandler DateTimePick.ValueChanged, AddressOf DateTimePicker2_ValueChanged

Open in new window

Private Sub DateTimePicker2_ValueChanged(ByVal sender As Object, ByVal e As EventArgs)
            Dim cControl As Control
            Dim counter As Int32 = 0
            Dim cControl2 As Control
            For Each cControl In Me.Panel2.Controls
                If TypeOf cControl Is DateTimePicker Then
                    counter += 1
                    If cControl.Name = "Start" & counter Then
                        Dim StartTime As DateTime = cControl.Text
                        If cControl.Name = "End" & counter Then
                            Dim EndTime As DateTime = cControl.Text
                            Dim Duration As TimeSpan = EndTime - StartTime
                            Dim Duration2 = Duration.TotalHours
                            For Each cControl2 In Me.Panel2.Controls
                                If TypeOf cControl2 Is TextBox And cControl2.Name = "Hours" & counter Then
                                    cControl2.Text = Duration2
                                End If
                        End If
                    End If
                End If
        Catch ex As System.Exception
        End Try
    End Sub

Open in new window

Ryan RaganOffice ManagerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Éric MoreauSenior .Net ConsultantCommented:
you need to add 1 AddHandler for each controls and I only see 1.

Also, you are running for troubles if you declare a variable the same name as a control (Dim DateTimePicker As New DateTimePicker).
Ryan RaganOffice ManagerAuthor Commented:
Thank you for responding, do you have any suggestions on handling an unknown # of DateTimePickers.valuechanged that are dynamically loaded at runtime? All I want is to show the timespan between two datetimepickers when the value is changed. The issue is each employee gets a start and end time so a schedule can be mass produced. Thank you for any other suggestions,

Éric MoreauSenior .Net ConsultantCommented:
>>do you have any suggestions on handling an unknown # of DateTimePickers.valuechanged that are dynamically loaded at runtime?

This is easy, for each instance that you declare (Dim), you need to call the AddHandler to add the event handler. Your code was showing that you were only doing it for the last one.

But if we step back a bit, wouldn't it be more appropriate to show all your employees in a datagrid? Since you don't know how many you have, creating controls on the fly can be a pain. And what if you have more than what can fit on the screen?

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.