Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get selected value from DateTimePicker

I would like to get selected value from DateTimePicker in VB (If I select only day value then I would like to get only selected day value.)

enter image description here

In this image I have selected (blue marked) year value from this DateTimePicker. So I need only this year value.

In the case of TextBox I can get selected value using

TextEndTime.SelectedText

Is there any syntax or approach to get selected value from DateTimePicker?

like image 926
Mir Shakawat Ali Jibon Avatar asked Aug 30 '18 10:08

Mir Shakawat Ali Jibon


People also ask

How do I get the Datepicker value?

If you want to get the date when the user selects it, you can do this: $("#datepicker"). datepicker({ onSelect: function() { var dateObject = $(this). datepicker('getDate'); } });

What is Datetimepicker (); in JS?

The JavaScript DateTime Picker is a lightweight and mobile-friendly control that allows end users to enter or select date and time values from a pop-up calendar and drop-down time list. It provides month, year, and decade views for quick navigation to the desired date.


1 Answers

As the DateTimePicker-control can be manipulated by using the arrow keys, you can use SendKeys to change the current selected value.

The following example gets the current DateTime-value of the DateTimePicker and, after sending the key, compares the value to the new value. At last it resets the DateTimePicker to the original value.
So the variable currSelected will contain the last Selection.

Dim currVal As DateTime
Dim newVal As DateTime
Dim valCheck As Boolean
Dim currSelected As Selection = Selection.None

Public Enum Selection
    None = 0
    Year = 1
    Month = 2
    Day = 3
End Enum

Private Sub CheckDTPSelection(dtp As DateTimePicker)
    valCheck = True
    currVal = dtp.Value
    SendKeys.Send("{UP}")
End Sub

Sub RefreshSelection(dtp As DateTimePicker)
    If valCheck Then
        newVal = dtp.Value

        If currVal.Year <> newVal.Year Then
            currSelected = Selection.Year
        ElseIf currVal.Month <> newVal.Month Then
            currSelected = Selection.Month
        ElseIf currVal.Day <> newVal.Day Then
            currSelected = Selection.Day
        End If

        dtp.Value = currVal
        valCheck = False
    End If
End Sub

Private Sub MyDateTimePicker_DropDown(sender As Object, e As EventArgs) Handles MyDateTimePicker.DropDown
    RemoveHandler MyDateTimePicker.MouseUp, AddressOf MyDateTimePicker_MouseUp
End Sub

Private Sub MyDateTimePicker_CloseUp(sender As Object, e As EventArgs) Handles MyDateTimePicker.CloseUp
    AddHandler MyDateTimePicker.MouseUp, AddressOf MyDateTimePicker_MouseUp
    CheckDTPSelection(MyDateTimePicker)
End Sub

Private Sub MyDateTimePicker_KeyUp(sender As Object, e As KeyEventArgs) Handles MyDateTimePicker.KeyUp
    If e.KeyValue = Keys.Left OrElse e.KeyValue = Keys.Right Then
        CheckDTPSelection(MyDateTimePicker)
    End If
End Sub

Private Sub MyDateTimePicker_MouseUp(sender As Object, e As MouseEventArgs) Handles MyDateTimePicker.MouseUp
    CheckDTPSelection(MyDateTimePicker)
End Sub

Private Sub MyDateTimePicker_ValueChanged(sender As Object, e As EventArgs) Handles MyDateTimePicker.ValueChanged
    Dim dtp As DateTimePicker = DirectCast(sender, DateTimePicker)

    RefreshSelection(dtp)
End Sub

Private Sub Btn_WhatsSelected_Click(sender As Object, e As EventArgs) Handles Btn_WhatsSelected.Click
    'Show the current selected value in a MessageBox
    MessageBox.Show(currSelected.ToString())
End Sub
like image 110
MatSnow Avatar answered Nov 03 '22 12:11

MatSnow