none
winform中的datetimepicker控件左右移动问题 RRS feed

  • 问题

  • 最近做一个VSOT的小项目,在用到datetimepicker控件时,在设置valuechanged事件里有两个函数,这两个函数都会调用timepicker的value作为参数。不知道为什么直接下拉的日历选择日期没有任何问题,但是使用日历上的左右移动月份时就会一直移动。例如我右移选择下一月,他会一直下一个月停不下来。求助解惑。

     private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
            {
               
                this.panel1.Refresh();
            }

     private void panel1_Paint(object sender, PaintEventArgs e)
            {
               
                Painter paint = new Painter();
                paint.PaintCalendar(e.Graphics);
               
                List<Event> eList = GetAllEvents(CovTime.OrgTime(dateTimePicker1.Value),dateTimePicker1.Value.AddDays(2));
                foreach (Event evt in eList)
                {
                    paint.DrawEventStatus(e.Graphics, evt,this.dateTimePicker1.Value);
                }
            }

    2016年3月30日 2:41

答案

  • 出现了死循环调用了吧。dateTimePicker1.Value.AddDays(2)引发了dateTimePicker1_ValueChanged,反过来dateTimePicker1_ValueChanged又引发了dateTimePicker1.Value.AddDays(2)。不要用dateTimePicker1.Value.AddDays(2)这句,先用一个DateTime型变量把dateTimePicker1.Value存下来,然后对这个变量AddDays(2),试试看。

    努力~

    2016年3月30日 13:37

全部回复

  • Hi liuwen,
    你的代码无法使用,在我这边缺少几个类:
                paint.PaintCalendar(e.Graphics);
                List<Event> eList = GetAllEvents(CovTime.OrgTime(dateTimePicker1.Value), dateTimePicker1.Value.AddDays(2));
                paint.DrawEventStatus(e.Graphics, evt, this.dateTimePicker1.Value);
    另外,在你的代码中和日期控件的值变化有关联的是下面那行代码,我推测问题可能和它有关:
    GetAllEvents(CovTime.OrgTime(dateTimePicker1.Value),dateTimePicker1.Value.AddDays(2));
    Regards,
    Moonlight

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    2016年3月30日 8:22
  • 出现了死循环调用了吧。dateTimePicker1.Value.AddDays(2)引发了dateTimePicker1_ValueChanged,反过来dateTimePicker1_ValueChanged又引发了dateTimePicker1.Value.AddDays(2)。不要用dateTimePicker1.Value.AddDays(2)这句,先用一个DateTime型变量把dateTimePicker1.Value存下来,然后对这个变量AddDays(2),试试看。

    努力~

    2016年3月30日 13:37