如何在VB.Net中控制DateTimePicker的ShowUpDown

时间:2013-01-14 14:50:47

标签: vb.net datetimepicker

我的表单中有两个DateTimePicker对象,

“hh:mm tt”的自定义格式

ShowUpDown = True

离。

Time : 12:00

当我按向上键时,我想将分钟值增加到+30

Time : 12:30 PM

1 个答案:

答案 0 :(得分:0)

使用活动

DataTimePicket.KeyDown

样本用法:

<强> VB.NET

Private Sub Form1_Load(sender As Object, e As EventArgs)

    dateTimePicker1.KeyDown += DateTimePicker1OnKeyUp

End Sub

Private Sub DateTimePicker1OnKeyUp(sender As Object, e As KeyEventArgs)
    If e.KeyValue = 38 Then
        ' up
        dateTimePicker1.Value = dateTimePicker1.Value.AddSeconds(30)
        e.SuppressKeyPress = True
    ElseIf e.KeyValue = 40 Then
        'down
        dateTimePicker1.Value = dateTimePicker1.Value.AddSeconds(-30)
        e.SuppressKeyPress = True
    End If
End Sub

<强> C#

    private void Form1_Load(object sender, EventArgs e)
    {

        dateTimePicker1.KeyDown += DateTimePicker1OnKeyUp;

    }

    private void DateTimePicker1OnKeyUp(object sender, KeyEventArgs e)
    {
        if (e.KeyValue == 38) // up
        {
            dateTimePicker1.Value = dateTimePicker1.Value.AddSeconds(30);
            e.SuppressKeyPress = true;
        }
        else if (e.KeyValue == 40) //down
        {
            dateTimePicker1.Value = dateTimePicker1.Value.AddSeconds(-30);
            e.SuppressKeyPress = true;
        }
    }