[RESOLVED] [2005] Set Value of Each DateTimePicker In a Groupbox
Hello,
I have several DateTimePickers on my form (formatted as time) and would like to populate each of them based on the current hour.
e.g. If I open the form at 10:22 AM I would lke the datetimepickers to show 10:00.
In my own mind, the code below is the sort of thing I'm after :
Code:
Dim d As DateTimePicker
For Each d In GroupBox1
d.Value = Date.Today & " " & Hour(Now) & ":00"
Next
However I get a message about groupbox1 not being a collection type.
I assume there is a way of doing what I want ?
Re: [2005] Set Value of Each DateTimePicker In a Groupbox
Put something like this in your form load event handler
Code:
Dim time As Date = DateTime.Now.AddMinutes(-DateTime.Now.Minute)
Dim DTP As DateTimePicker = Nothing
For Each ctrl As Control In Me.GroupBox1.Controls
If TypeOf ctrl Is DateTimePicker Then
DTP = DirectCast(ctrl, DateTimePicker)
DTP.Value = time
End If
Next
Re: [2005] Set Value of Each DateTimePicker In a Groupbox
This:
vb.net Code:
Dim time As Date = DateTime.Now.AddMinutes(-DateTime.Now.Minute)
doesn't allow for seconds and milliseconds. This would be better:
vb.net Code:
Dim time As Date = DateTime.Today.AddHours(DateTime.Now.Hour)
Re: [2005] Set Value of Each DateTimePicker In a Groupbox
Quote:
Originally Posted by jmcilhinney
This:
vb.net Code:
Dim time As Date = DateTime.Now.AddMinutes(-DateTime.Now.Minute)
doesn't allow for seconds and milliseconds. This would be better:
vb.net Code:
Dim time As Date = DateTime.Today.AddHours(DateTime.Now.Hour)
What?! :confused: Care to explain?
1 Attachment(s)
Re: [2005] Set Value of Each DateTimePicker In a Groupbox
The first code rounds the minutes but leaves the seconds and milliseconds so you don't get the even hour. My code gives you the even hour.
Re: [2005] Set Value of Each DateTimePicker In a Groupbox
Thank you both very much - I appreciate it.