关于DateTimePicker控件ValueChanged事件的问题,在线等!!!!
各位:我做了一个简单的程序,即:有2个DateTimePicker控件,datetimepicker2要比1的时间晚,当选择比datetimepicker1早的时间时给出提示错误,同时让datetimepicker2值为datetimepicker1的值加1天。但是,每次程序运行时,每次设置datetimepicker2比1早时,总是出现两次提示错误,不知道怎么回事,请大家执教。程序如下:
using System;
namespace WindowsApplication1
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.DateTimePicker dateTimePicker1;
private System.Windows.Forms.DateTimePicker dateTimePicker2;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
InitializeComponent();
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.dateTimePicker1 = new System.Windows.Forms.DateTimePicker();
this.dateTimePicker2 = new System.Windows.Forms.DateTimePicker();
this.SuspendLayout();
//
// dateTimePicker1
//
this.dateTimePicker1.Location = new System.Drawing.Point(48, 64);
this.dateTimePicker1.Name = "dateTimePicker1";
this.dateTimePicker1.TabIndex = 0;
//
// dateTimePicker2
//
this.dateTimePicker2.Location = new System.Drawing.Point(48, 112);
this.dateTimePicker2.Name = "dateTimePicker2";
this.dateTimePicker2.TabIndex = 0;
this.dateTimePicker2.ValueChanged += new System.EventHandler(this.dateTimePicker2_ValueChanged);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.dateTimePicker1);
this.Controls.Add(this.dateTimePicker2);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void dateTimePicker2_ValueChanged(object sender, System.EventArgs e)
{
DateTime advDate = (sender as DateTimePicker).Value;
if (advDate <= this.dateTimePicker1.Value)
{
MessageBox.Show("2的日期不能早于1的日期!","提示",MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
this.dateTimePicker2.Value = this.dateTimePicker1.Value.AddDays(1);
}
}
}
}
我的开发环境是2003,framework1.1