-1

当用户单击 DateTimePicker 上的“转到今天”按钮时,如何捕获单击事件?

谢谢。

4

2 回答 2

0

只需处理DateTimePicker控件的ValueChanged事件并检查所选日期是否为今天的日期。像这样 -

    // ValueChanged Event
    private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
    {
        if (dateTimePicker1.Value.Date == DateTime.Now.Date)
        {
            MessageBox.Show("Hola !!");
        }
    }
于 2013-01-18T06:53:28.903 回答
0

我正在使用 win xp 和 .net 3.5。您可以使用鼠标按下事件,如果您单击控件本身和“今天”,它将被触发,但如果您在“今天”以外的任何地方单击下拉日历,则不会触发。为了避免在我单击控件本身时触发事件,我使用了 return 语句。此事件仅在您单击“今天”时设置实际时间。不幸的是,在win8中,mousedown事件没有被触发......

    private void dateTimePicker2_MouseDown(object sender, MouseEventArgs e)
    {
        Point pt = dateTimePicker2.Location;
        Point mouse_pt = dateTimePicker2.Parent.PointToClient(Cursor.Position);
        int diff_x = mouse_pt.X - pt.X;
        int diff_y = mouse_pt.Y - pt.Y;
        if (diff_y < dateTimePicker2.Size.Height || diff_x > dateTimePicker2.Size.Width )
            return;
        DateTime tim_cal = dateTimePicker2.Value;
        DateTime now = DateTime.Now;
        TimeSpan ts = new TimeSpan(now.Hour, now.Minute, now.Second);
        tim_cal = tim_cal.Date + ts;
        dateTimePicker2.Value = tim_cal;
    }
于 2013-04-08T12:11:21.220 回答