none
All Datetimepickers in form RRS feed

  • Question

  • Hello,

    I am using this to set the Datetimepicker to customformat and clear with start programm

    in Formload
     DateTimePicker11.CustomFormat = " "
            DateTimePicker11.Format = DateTimePickerFormat.[Custom]

    then when i select the Datetimepicker

       Private Sub DateTimePicker11_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DateTimePicker11.ValueChanged
            If DateTimePicker11.CustomFormat = " " Then
                DateTimePicker11.CustomFormat = "dd-MM-yyyy"
            End If
            
        End Sub

    But there are some 100 Datetimepickers on the form, how do i use this in Formload ?

    This is not working.

     For Each d As Control In Me.Controls.OfType(Of DateTimePicker)()
                DateTimePicker.CustomFormat = " "
                DateTimePicker.Format = DateTimePickerFormat.[Custom]
            Next
            

    Thursday, June 14, 2018 12:04 PM

Answers

  • One handler for all DateTimePickers should do it

        Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
            SetDateTimePickerFormat() 'set all to " " format
        End Sub
    
        Private Sub SetDateTimePickerFormat(Optional format As String = " ",
                                            Optional container As Control = Nothing)
    
            If container Is Nothing Then container = Me
            For Each c As Control In container.Controls()
                If TypeOf c Is DateTimePicker Then
                    Dim d As DateTimePicker = DirectCast(c, DateTimePicker)
                    d.CustomFormat = format
                    d.Format = DateTimePickerFormat.Custom
                    If d.Tag Is Nothing Then
                        'only once
                        AddHandler d.ValueChanged, AddressOf DateTimePickerS_ValueChanged
                        d.Tag = "has handler"
                    End If
                ElseIf c.Controls.Count > 0 Then 'container 
                    'yes
                    SetDateTimePickerFormat(format, c)
                    'Else
                    'no
                    '    Debug.WriteLine(c.Name)
                End If
            Next
        End Sub
    
        Private Sub DateTimePickerS_ValueChanged(sender As Object,
                                                 e As EventArgs)
    
            Dim dtp As DateTimePicker = DirectCast(sender, DateTimePicker)
            If dtp.CustomFormat.Trim = "" Then
                dtp.CustomFormat = "dd-MM-yyyy"
            End If
        End Sub
    


    "Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it."

    - from former MSDN User JohnWein

    SerialPort Info

    Multics - An OS ahead of its time.

    • Marked as answer by KeesBlunder Friday, June 15, 2018 9:17 AM
    Thursday, June 14, 2018 8:17 PM

All replies

  • That code shouldn't even compile... the loop should be:

    For Each d As DateTimePicker In Me.Controls.OfType(Of DateTimePicker)()
        d.CustomFormat = " "
        d.Format = DateTimePickerFormat.[Custom]
    Next
    

    This assumes that all of the controls are on the Form directly and not located in other container controls such as a Panel or GroupBox.  If you have other container controls then this loop needs to be placed into a recursive function that can check all controls for sub-controls:

    Private Sub SetDateTimePickerFormat(Optional container As Control = Nothing)
        If container Is Nothing Then container = Me
        For Each c In container.Controls()
            If TypeOf c Is DateTimePicker Then
                Dim d = DirectCast(c, DateTimePicker)
                d.CustomFormat = " "
                d.Format = DateTimePickerFormat.[Custom]
            ElseIf TypeOf c Is ContainerControl Then
                SetDateTimePickerFormat(c)
            End If
        Next
    End Sub
    
    Then in Form.Load you can just call SetDateTimePickerFormat().


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    • Proposed as answer by Gtripodi Thursday, June 14, 2018 3:14 PM
    Thursday, June 14, 2018 12:12 PM
    Moderator
  • Hello,

    If using VS2015 or higher this would be an option. The beauty is this is usable in other forms or classes.

    Public Module Extensions
        <Runtime.CompilerServices.Extension>
        Public Iterator Function Descendants(Of T As Class)(control As Control) As IEnumerable(Of T)
            For Each child As Control In control.Controls
                Dim childOfT As T = TryCast(child, T)
                If childOfT IsNot Nothing Then
                    Yield CType(childOfT, T)
                End If
                If child.HasChildren Then
                    For Each descendant As T In child.Descendants(Of T)()
                        Yield descendant
                    Next
                End If
            Next
        End Function
    End Module
    

    Form code

    Public Class Form3
        Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Me.Descendants(Of DateTimePicker)().
                ToList().
                ForEach(
                Sub(picker)
                    picker.CustomFormat = " "
                    picker.Format = DateTimePickerFormat.[Custom]
                End Sub)
        End Sub
    End Class


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Thursday, June 14, 2018 1:38 PM
    Moderator
  • That code shouldn't even compile... the loop should be:

    For Each d As DateTimePicker In Me.Controls.OfType(Of DateTimePicker)()
        d.CustomFormat = " "
        d.Format = DateTimePickerFormat.[Custom]
    Next
    

    This assumes that all of the controls are on the Form directly and not located in other container controls such as a Panel or GroupBox.  If you have other container controls then this loop needs to be placed into a recursive function that can check all controls for sub-controls:

    Private Sub SetDateTimePickerFormat(Optional container As Control = Nothing)
        If container Is Nothing Then container = Me
        For Each c In container.Controls()
            If TypeOf c Is DateTimePicker Then
                Dim d = DirectCast(c, DateTimePicker)
                d.CustomFormat = " "
                d.Format = DateTimePickerFormat.[Custom]
            ElseIf TypeOf c Is ContainerControl Then
                SetDateTimePickerFormat(c)
            End If
        Next
    End Sub
    
    Then in Form.Load you can just call SetDateTimePickerFormat().


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Reed, I am no expert, but is your code employing linq? Am I wrong in thinking that is an unnecessary layer and could potentially add overhead? Would this not be a less expensive way to do it (even if by an infinitesimal amount)

            For Each ctrl As Control In Controls
                If TypeOf ctrl Is DateTimePicker Then
                    With DirectCast(ctrl, DateTimePicker)
                        .CustomFormat = String.Empty
                        .Format = DateTimePickerFormat.Custom
                    End With
                End If
            Next


    Live as if you were going to die today, learn as if you were going to live forever -Mahatma Gandhi

    Thursday, June 14, 2018 3:21 PM
  • That code shouldn't even compile... the loop should be:

    For Each d As DateTimePicker In Me.Controls.OfType(Of DateTimePicker)()
        d.CustomFormat = " "
        d.Format = DateTimePickerFormat.[Custom]
    Next

    This assumes that all of the controls are on the Form directly and not located in other container controls such as a Panel or GroupBox.  If you have other container controls then this loop needs to be placed into a recursive function that can check all controls for sub-controls:

    Private Sub SetDateTimePickerFormat(Optional container As Control = Nothing)
        If container Is Nothing Then container = Me
        For Each c In container.Controls()
            If TypeOf c Is DateTimePicker Then
                Dim d = DirectCast(c, DateTimePicker)
                d.CustomFormat = " "
                d.Format = DateTimePickerFormat.[Custom]
            ElseIf TypeOf c Is ContainerControl Then
                SetDateTimePickerFormat(c)
            End If
        Next
    End Sub
    Then in Form.Load you can just call SetDateTimePickerFormat().


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Reed, I am no expert, but is your code employing linq? Am I wrong in thinking that is an unnecessary layer and could potentially add overhead? Would this not be a less expensive way to do it (even if by an infinitesimal amount)

            For Each ctrl As Control In Controls
                If TypeOf ctrl Is DateTimePicker Then
                    With DirectCast(ctrl, DateTimePicker)
                        .CustomFormat = String.Empty
                        .Format = DateTimePickerFormat.Custom
                    End With
                End If
            Next


    Live as if you were going to die today, learn as if you were going to live forever -Mahatma Gandhi

    Hi Garry,

    No, there's no LINQ in my example.  In fact, the code you posted is exactly the same thing, barring the use of With which just implicitly creates the "d" variable shown in my example, and barring the fact that my example was recursive to get any controls within child controls of the form.  The actual For-Each loops though are essentially the same.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Thursday, June 14, 2018 3:26 PM
    Moderator
  • That code shouldn't even compile... the loop should be:

    For Each d As DateTimePicker In Me.Controls.OfType(Of DateTimePicker)()
        d.CustomFormat = " "
        d.Format = DateTimePickerFormat.[Custom]
    Next

    This assumes that all of the controls are on the Form directly and not located in other container controls such as a Panel or GroupBox.  If you have other container controls then this loop needs to be placed into a recursive function that can check all controls for sub-controls:

    Private Sub SetDateTimePickerFormat(Optional container As Control = Nothing)
        If container Is Nothing Then container = Me
        For Each c In container.Controls()
            If TypeOf c Is DateTimePicker Then
                Dim d = DirectCast(c, DateTimePicker)
                d.CustomFormat = " "
                d.Format = DateTimePickerFormat.[Custom]
            ElseIf TypeOf c Is ContainerControl Then
                SetDateTimePickerFormat(c)
            End If
        Next
    End Sub
    Then in Form.Load you can just call SetDateTimePickerFormat().


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Reed, I am no expert, but is your code employing linq? Am I wrong in thinking that is an unnecessary layer and could potentially add overhead? Would this not be a less expensive way to do it (even if by an infinitesimal amount)

            For Each ctrl As Control In Controls
                If TypeOf ctrl Is DateTimePicker Then
                    With DirectCast(ctrl, DateTimePicker)
                        .CustomFormat = String.Empty
                        .Format = DateTimePickerFormat.Custom
                    End With
                End If
            Next


    Live as if you were going to die today, learn as if you were going to live forever -Mahatma Gandhi

    Hi Garry,

    No, there's no LINQ in my example.  In fact, the code you posted is exactly the same thing, barring the use of With which just implicitly creates the "d" variable shown in my example, and barring the fact that my example was recursive to get any controls within child controls of the form.  The actual For-Each loops though are essentially the same.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Ok, thanks for clearing that up. I shall adopt your method moving forward. Im not real sure what made me think linq was involved

    Live as if you were going to die today, learn as if you were going to live forever -Mahatma Gandhi

    Thursday, June 14, 2018 3:32 PM
  • This is reeds code modified slightly

        Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
            SetDateTimePickerFormat() 'set all to " " format
    
            'or
    
            SetDateTimePickerFormat("dd-MM-yyyy") 'set all to "dd-MM-yyyy" format
        End Sub
    
        Private Sub SetDateTimePickerFormat(Optional format As String = " ",
                                            Optional container As Control = Nothing)
    
            If container Is Nothing Then container = Me
            For Each c As Control In container.Controls()
                If TypeOf c Is DateTimePicker Then
                    Dim d As DateTimePicker = DirectCast(c, DateTimePicker)
                    d.CustomFormat = format
                    d.Format = DateTimePickerFormat.Custom
                ElseIf c.Controls.Count > 0 Then 'container 
                    'yes
                    SetDateTimePickerFormat(format, c)
                    'Else
                    'no
                    '    Debug.WriteLine(c.Name)
                End If
            Next
        End Sub
    


    "Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it."

    - from former MSDN User JohnWein

    SerialPort Info

    Multics - An OS ahead of its time.

    Thursday, June 14, 2018 3:42 PM
  • Hello Karen,

    i am using the 2010 version

    Thursday, June 14, 2018 6:14 PM
  • Thanks this is working.

    I use the  SetDateTimePickerFormat() for the first part

    Is it possible to adjust it so when i use the DateTimePicker.ValueChanged event

    the custom format is working

      If DateTimePicker11.CustomFormat = " " Then
                DateTimePicker11.CustomFormat = "dd-MM-yyyy"

    Thursday, June 14, 2018 6:19 PM
  • Hello Karen,

    i am using the 2010 version

    Then my solution will not work for you as iterator methods in tangent with Yield are not available in VS2010.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Thursday, June 14, 2018 6:25 PM
    Moderator
  • One handler for all DateTimePickers should do it

        Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
            SetDateTimePickerFormat() 'set all to " " format
        End Sub
    
        Private Sub SetDateTimePickerFormat(Optional format As String = " ",
                                            Optional container As Control = Nothing)
    
            If container Is Nothing Then container = Me
            For Each c As Control In container.Controls()
                If TypeOf c Is DateTimePicker Then
                    Dim d As DateTimePicker = DirectCast(c, DateTimePicker)
                    d.CustomFormat = format
                    d.Format = DateTimePickerFormat.Custom
                    If d.Tag Is Nothing Then
                        'only once
                        AddHandler d.ValueChanged, AddressOf DateTimePickerS_ValueChanged
                        d.Tag = "has handler"
                    End If
                ElseIf c.Controls.Count > 0 Then 'container 
                    'yes
                    SetDateTimePickerFormat(format, c)
                    'Else
                    'no
                    '    Debug.WriteLine(c.Name)
                End If
            Next
        End Sub
    
        Private Sub DateTimePickerS_ValueChanged(sender As Object,
                                                 e As EventArgs)
    
            Dim dtp As DateTimePicker = DirectCast(sender, DateTimePicker)
            If dtp.CustomFormat.Trim = "" Then
                dtp.CustomFormat = "dd-MM-yyyy"
            End If
        End Sub
    


    "Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it."

    - from former MSDN User JohnWein

    SerialPort Info

    Multics - An OS ahead of its time.

    • Marked as answer by KeesBlunder Friday, June 15, 2018 9:17 AM
    Thursday, June 14, 2018 8:17 PM
  • Here is another twist, hopefully it works with VS2010.

    Private Sub DateTimePicker_ValueChanged(sender As Object, e As EventArgs)
        Dim dtp = CType(sender, DateTimePicker)
        Console.WriteLine(dtp.Name & " " & dtp.Value.ToString())
    End Sub
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim ctrl As Control = Me.GetNextControl(Me, True)
        Do Until ctrl Is Nothing
            If TypeOf ctrl Is DateTimePicker Then
                Dim dtp = CType(ctrl, DateTimePicker)
                AddHandler dtp.ValueChanged, AddressOf DateTimePicker_ValueChanged
                If dtp.CustomFormat = "" Then
                    dtp.CustomFormat = "dd-MM-yyyy"
                End If
            End If
            ctrl = Me.GetNextControl(ctrl, True)
        Loop
    End Sub
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Thursday, June 14, 2018 10:32 PM
    Moderator
  • Thanks ,
    Friday, June 15, 2018 9:17 AM
  • I take a look at it.

    Thanks

    Friday, June 15, 2018 9:18 AM