从DateTimePicker获取选定的值

时间:2018-08-30 10:28:38

标签: vb.net winforms datetimepicker

我想从VB中的DateTimePicker获取选定的值(如果我仅选择日期值,那么我仅想获取选定的日期值。)

enter image description here

在此图像中,我从此DateTimePicker中选择了(蓝色标记)年份值。所以我只需要今年的值。

对于TextBox,我可以使用

TextEndTime.SelectedText

是否有任何语法或方法可以从DateTimePicker获取所选值?

3 个答案:

答案 0 :(得分:3)

由于可以使用箭头键来操作DateTimePicker控件,因此可以使用SendKeys来更改当前选择的值。

下面的示例获取DateTime的当前DateTimePicker值,并在发送键之后,将该值与新值进行比较。最后,它将DateTimePicker重置为原始值。
因此变量currSelected将包含最后一个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

答案 1 :(得分:0)

大家好,谢谢您的提示,可以操纵DateTimePicker控件。 我在DateTimePicker上遇到选择问题,当前选择的项目值无法发送到文本框,因为DTP仅工作valuechanged事件。我花了4个小时的时间找到解决方案,并编写了以下代码:

Public MyEventCounter As Integer = 0

Private Sub DTPAcquDt_DropDown(sender As Object, e As EventArgs) Handles DTPAcquDt.DropDown
        RemoveHandler DTPAcquDt.MouseUp, AddressOf dtpacqudt_closeup
End Sub
Private Sub dtpacqudt_closeup(sender As Object, e As EventArgs) Handles DTPAcquDt.CloseUp
        AddHandler DTPAcquDt.MouseUp, AddressOf dtpacqudt_closeup
        'Check the Mouse/Keys event counter
        If MyEventCounter > 0 Then
            TxtDtAcqu.Text = DTPAcquDt.Value
            'RESET The Counter
            MyEventCounter = 0
        End If
End Sub
Private Sub DTPAcquDt_KeyUp(sender As Object, e As KeyEventArgs) Handles DTPAcquDt.KeyUp
        If e.KeyValue = Keys.Left OrElse e.KeyValue = Keys.Right Then
            MyEventCounter = MyEventCounter + 1
        End If
End Sub
Private Sub DTPAcquDt_MouseUp(sender As Object, e As MouseEventArgs) Handles DTPAcquDt.MouseUp
        MyEventCounter = MyEventCounter + 1
End Sub
Private Sub DTPAcquDt_ValueChanged(sender As Object, e As EventArgs) Handles DTPAcquDt.ValueChanged
        TxtDtAcqu.Text = DTPAcquDt.Value
        'RESET The Counter
        MyEventCounter = 0
End Sub

答案 2 :(得分:-5)

您需要根据值创建DateTime对象。

DateTime selectedDate = new DataTime(TextEndTime.Value);
int year = selectedDate.Year;

VB版本。

Dim selectedDate As DateTime = New DataTime(TextEndTime.Value)
Dim year As Integer = selectedDate.Year