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.)
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
?
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'); } });
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With