关于DateTimePicker控件ValueChanged事件的问题,在线等!!!!

welspring 2006-10-11 11:00:16
各位:我做了一个简单的程序,即:有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
...全文
211 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
welspring 2006-10-11
  • 打赏
  • 举报
回复
加1之后已经比dateTimePicker1的值大了阿,第二次引发该事件不应当在谈出提示消息了,但是为什么谈出呢?你式一下。
真相重于对错 2006-10-11
  • 打赏
  • 举报
回复
bks
this.dateTimePicker2.Value = this.dateTimePicker1.Value.AddDays(1);
dugupiaoyun 2006-10-11
  • 打赏
  • 举报
回复
关于你的连续2次弹出messagebox的问题,解决如下,将事件代码移动到dateTimePicker2_CloseUp这个事件里,就完全解决了,即将你的代码改为:

private void dateTimePicker2_CloseUp(object sender, System.EventArgs e)
{
DateTime advDate = (sender as DateTimePicker).Value;
int ret=DateTime.Compare(advDate,this.dateTimePicker1.Value);
if (ret==-1)
{
MessageBox.Show("2的日期不能早于1的日期!","提示",MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
this.dateTimePicker2.Value =this.dateTimePicker1.Value.AddDays(1);
}
dugupiaoyun 2006-10-11
  • 打赏
  • 举报
回复
我在你的另外一个帖子里已经回复过了的
dugupiaoyun 2006-10-11
  • 打赏
  • 举报
回复
把你的代码中判断改成如下:
private void dateTimePicker2_ValueChanged(object sender, System.EventArgs e)
{
DateTime advDate = (sender as DateTimePicker).Value;
int ret=DateTime.Compare(advDate,this.dateTimePicker1.Value);
if (ret==-1)
{
MessageBox.Show("2的日期不能早于1的日期!","提示",MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
this.dateTimePicker2.Value = this.dateTimePicker1.Value.AddDays(1);
}
}

8,834

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 组件/控件开发
社区管理员
  • 组件/控件开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧