articles

DateTimePicker Control in VB.Net

DateTimePicker Control in VB.Net

Pushpendra Singh 34220 14-Dec-2010

A DateTimePicker control allows you to select a date and time.

Drag and drop DateTimePicker control form toolbox on window Form.

DateTimePicker Control in VB.Net

Code:

' DateTimePicker ValueChanged Event Hander
Private Sub DateTimePicker1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DateTimePicker1.ValueChanged
        'Display the selected Date in Label
        Label2.Text = DateTimePicker1.Text
    End Sub

 

Run the project

DateTimePicker Control in VB.Net

When you click on the calendar icon in the control then calendar popup will show.

DateTimePicker Control in VB.Net

When you select the date then ValueChanged event will fire and selected date will be show in Label.

DateTimePicker Control in VB.Net

DateTimePicker Properties:

Format: we can set DateTimePicker Format in which Format it should show.

  Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Display the date in Short Format
        DateTimePicker1.Format = DateTimePickerFormat.Short
    End Sub

At run time DateTimePicker value will show in short Format.

DateTimePicker Control in VB.Net

Show checkbox:

You can make a DateTimePicker control editable where you can change the value without using the Calendar. This can be done by setting ShowCheckBox property to true. 

     Private Sub Form2_Load(ByVal sender  As System.Object, ByVal e As System.EventArgs)  Handles MyBase.Load
        ' Makes DateTimePicker editable
        DateTimePicker1.ShowCheckBox = True   
    End Sub


DateTimePicker Control in VB.Net

Checkbox will show in DateTimePicker.You can edit the value in DateTimePicker and changed value will show in Label.

ShowUpDown

We can also use an up-down control to set the date and time.

Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
       
        'UpDown control will show in DateTimePicker
        DateTimePicker1.ShowUpDown = True
    End Sub


DateTimePicker Control in VB.Net

UpDown Control   is display in DateTimePicker.we can change date through Updown.


Updated 09-Apr-2020

Leave Comment

Comments

Liked By