user1030181 user1030181 - 7 months ago 56
Vb.net Question

How to add current date and time on this button click on windows form

I am using windows forms application and creating some controls dynamically on form load.

Controls created are Label, UltraDateTimeEditor and Button.

I am using the below code to implement that.

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

Dim n = 5
For i As Integer = 0 To n - 1
'Create label
Dim label As New Label()
label.Text = [String].Format("Label {0}", i)
'Position label on screen
label.Left = 50
label.Top = (i + 1) * 30
label.Width = 70

'Create UltraDateTimeEditor
Dim dtpicker = New UltraDateTimeEditor
dtpicker.Name = "Date" + i.ToString()
dtpicker.MaskInput = "{date} hh:mm"
dtpicker.MaskDisplayMode = MaskMode.IncludeBoth
dtpicker.SpinButtonDisplayStyle = Infragistics.Win.ButtonDisplayStyle.Always
dtpicker.Left = 120
dtpicker.Top = (i + 1) * 30




Dim button As New Button()
'Position textbox on screen
button.Text = "Now"
button.Left = 290
button.Top = (i + 1) * 30
'Add controls to form
Me.Panel1.Controls.Add(label)
Me.Panel1.Controls.Add(dtpicker)
Me.Panel1.Controls.Add(button)
Next

End Sub


The screen looks like this

enter image description here

Now when the click the Now button beside respective datetimeeditor i need to put the current date and time into respective datetimeeditor.
Please suggest how can i do this.

Answer

You can use AddHandler to your dynamic controls for the Button so when they are clicked you can run some code... Please note the: AddHandler section...

Code has been tried and Tested

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Dim n = 5
        For i As Integer = 0 To n - 1
            'Create label
            Dim label As New Label()
            label.Text = [String].Format("Label {0}", i)
            'Position label on screen
            label.Left = 50
            label.Top = (i + 1) * 30
            label.Width = 70

            Dim dtpicker = New UltraDateTimeEditor
            dtpicker.Name = "Date" + i.ToString()
            dtpicker.MaskInput = "{date} hh:mm"
            dtpicker.MaskDisplayMode = MaskMode.IncludeBoth
            dtpicker.SpinButtonDisplayStyle = Infragistics.Win.ButtonDisplayStyle.Always
            dtpicker.Left = 120
            dtpicker.Top = (i + 1) * 30
            dtpicker.Tag = i 'Set this as we will use this for comparison



            Dim button As New Button()
            'Position textbox on screen
            button.Text = "Now"
            button.Tag = i 'Set this as we will use this for comparison
            button.Left = 290
            button.Top = (i + 1) * 30
            'Add controls to form
            Me.Controls.Add(label)
            Me.Controls.Add(dtpicker)
            Me.Controls.Add(button)

            AddHandler button.Click, AddressOf UpdateTime

        Next

    End Sub

This method handles changing the DateTime object... We use the Button.Tag property and check it against the DateTime.Tag. Then we can set the time associated with that Button.

Private Sub UpdateTime(sender As System.Object, e As System.EventArgs)
        Dim nButton As Button = DirectCast(sender, Button)
        If nButton IsNot Nothing AndAlso nButton.Tag <> Nothing Then
            For Each ctrl In Me.Controls.OfType(Of UltraDateTimeEditor)()
                If ctrl.Tag = nButton.Tag Then
                    ctrl.Value = DateTime.UtcNow
                    Exit Sub
                End If
            Next
        End If

    End Sub
Comments