Results 1 to 6 of 6

Thread: [RESOLVED] [2005] Set Value of Each DateTimePicker In a Groupbox

  1. #1

    Thread Starter
    Hyperactive Member Jonny1409's Avatar
    Join Date
    Mar 2005
    Posts
    308

    Resolved [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 ?

  2. #2
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    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

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2005] Set Value of Each DateTimePicker In a Groupbox

    This:
    vb.net Code:
    1. Dim time As Date = DateTime.Now.AddMinutes(-DateTime.Now.Minute)
    doesn't allow for seconds and milliseconds. This would be better:
    vb.net Code:
    1. Dim time As Date = DateTime.Today.AddHours(DateTime.Now.Hour)
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: [2005] Set Value of Each DateTimePicker In a Groupbox

    Quote Originally Posted by jmcilhinney
    This:
    vb.net Code:
    1. Dim time As Date = DateTime.Now.AddMinutes(-DateTime.Now.Minute)
    doesn't allow for seconds and milliseconds. This would be better:
    vb.net Code:
    1. Dim time As Date = DateTime.Today.AddHours(DateTime.Now.Hour)
    What?! Care to explain?

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    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.
    Attached Images Attached Images  
    Last edited by jmcilhinney; Aug 20th, 2007 at 10:58 PM.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Hyperactive Member Jonny1409's Avatar
    Join Date
    Mar 2005
    Posts
    308

    Re: [2005] Set Value of Each DateTimePicker In a Groupbox

    Thank you both very much - I appreciate it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width