.net 日期时间选择器工作不正常
事实上,我在c sharp dot net 2008中有两个dateTimePicker。它们都执行相同的事件。但是其中一个在我使用另一个之前不能正常工作。请帮助我.net 日期时间选择器工作不正常,.net,.net,事实上,我在c sharp dot net 2008中有两个dateTimePicker。它们都执行相同的事件。但是其中一个在我使用另一个之前不能正常工作。请帮助我 private void dtpStart_ValueChanged(object sender, EventArgs e) { if (cmbDay.SelectedIndex == -1 || cmbLeaveName.SelectedIndex == -1) {
private void dtpStart_ValueChanged(object sender, EventArgs e)
{
if (cmbDay.SelectedIndex == -1 || cmbLeaveName.SelectedIndex == -1)
{
MessageBox.Show("Please select Day and Leave Name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
dtpStart.ValueChanged -= new EventHandler(dtpStart_ValueChanged);
}
if (dtpStart.Value > dtpEnd.Value)
{
MessageBox.Show("The End date of leave cannot be occur before date of leave ", "Invalid Entry", MessageBoxButtons.OK);
dtpStart.Value = dtpEnd.Value;
}
getdays();
check = validate();
if (check == "Incorrect")
{
check = "Correct";
return;
}
LoadDataGridView();
}
private void dtpEnd_ValueChanged(object sender, EventArgs e)
{
if (cmbDay.SelectedIndex == -1 || cmbLeaveName.SelectedIndex == -1)
{
MessageBox.Show("Please select Day and Leave Name","Error",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
dtpEnd.ValueChanged -= new EventHandler(dtpEnd_ValueChanged);
return;
}
if (dtpEnd.Value < dtpStart.Value)
{
MessageBox.Show("The End date of leave cannot be occur before date of leave ", "Invalid Entry", MessageBoxButtons.OK);
dtpEnd.Value = dtpStart.Value;
}
getdays();
check = validate();
if (check == "Incorrect")
{
check = "Correct";
return;
}
LoadDataGridView();
}
private void getdays()
{
double ts = GetDateDifference();
if (cmbDay.Text.ToString() == "Full Day")
{
txtLeaveApplied.Text = ts.ToString();
}
else if (cmbDay.Text.ToString() == "Half Day")
{
txtLeaveApplied.Text = ((float.Parse(ts.ToString())) / 2).ToString();
}
}
private string validate()
{
string Name = cmbApplicantName.Text.ToString();
string EMP_ID = GetEmpId(Name);
DataTable dtvalidate = new DataTable();
dtvalidate = LI.ValidateLeaveInfo(EMP_ID,
DateTime.Parse(dtpStart.Value.ToShortDateString()),
DateTime.Parse(dtpEnd.Value.ToShortDateString()));
if (dtvalidate.Rows.Count > 0)
{
StringBuilder date = new StringBuilder();
foreach (DataRow row in dtvalidate.Rows)
{
date.Append(row["Leave_Date"].ToString() + Environment.NewLine);
}
MessageBox.Show("Leave Already applied in following Date(s)" +
Environment.NewLine + date, "Select valid date", MessageBoxButtons.OK,
MessageBoxIcon.Exclamation);
check = "Incorrect";
dgvLeaveApplication.Rows.Clear();
}
dtvalidate.Rows.Clear();
dtvalidate.Dispose();
return check;
}
private void LoadDataGridView()
{
double ts = GetDateDifference();
dgvLeaveApplication.Rows.Clear();
DateTime dt = DateTime.Parse(dtpStart.Value.ToShortDateString());
for (int i = 0; i < Convert.ToInt32(ts.ToString()); i++)
{
dgvLeaveApplication.Rows.Add(dgvLeaveApplication.Rows.Count,
dt.ToShortDateString(), cmbLeaveName.SelectedValue.ToString());
dt = dt.AddDays(1);
}
dgvLeaveApplication.Refresh();
}
private void dtpStart\u值已更改(对象发送方,事件参数e)
{
如果(cmbDay.SelectedIndex=-1 | | cmbleevName.SelectedIndex=-1)
{
MessageBox.Show(“请选择日期并留下姓名”,“错误”,MessageBox按钮。确定,MessageBox图标。感叹号);
dtpStart.ValueChanged-=新事件处理程序(dtpStart\u ValueChanged);
}
if(dtpStart.Value>dtpEnd.Value)
{
MessageBox.Show(“休假结束日期不能出现在休假日期之前”,“输入无效”,MessageBox按钮.确定);
dtpStart.Value=dtpEnd.Value;
}
getdays();
检查=验证();
如果(检查==“不正确”)
{
check=“Correct”;
返回;
}
LoadDataGridView();
}
私有void dtpEnd_值已更改(对象发送方,事件参数e)
{
如果(cmbDay.SelectedIndex=-1 | | cmbleevName.SelectedIndex=-1)
{
MessageBox.Show(“请选择日期并留下姓名”,“错误”,MessageBox按钮。确定,MessageBox图标。感叹号);
dtpEnd.ValueChanged-=新事件处理程序(dtpEnd_ValueChanged);
返回;
}
if(dtpEnd.Value0)
{
StringBuilder日期=新建StringBuilder();
foreach(dtvalidate.Rows中的数据行)
{
date.Append(行[“Leave_date”].ToString()+环境.NewLine);
}
MessageBox.Show(“已在以下日期申请休假)”+
Environment.NewLine+date,“选择有效日期”,MessageBoxButtons.OK,
MessageBoxIcon.感叹号);
check=“不正确”;
dgvLeaveApplication.Rows.Clear();
}
dtvalidate.Rows.Clear();
dtvalidate.Dispose();
退货检查;
}
私有void LoadDataGridView()
{
双ts=GetDateDifference();
dgvLeaveApplication.Rows.Clear();
DateTime dt=DateTime.Parse(dtpStart.Value.ToShortDateString());
for(int i=0;i
检查这两个组件的AutoPostBack属性。您的代码中有一些非常奇怪的结构。例如:
check = validate();
if (check == "Incorrect")
{
check = "Correct";
return;
}
(为什么不让Validate()返回bool?为什么忽略验证错误?)
我的建议是重新构造、重命名和更改方法的签名,以便代码变得可读。这将使你更容易发现错误
您是否使用调试器逐步完成了代码?是否为两个选取者都引发了事件
编辑
您删除处理程序,但从不添加它。因此,在删除它之后,您必须决定何时再次添加它。那不是魔法能做到的 我需要在windows应用程序中而不是在web应用程序中删除处理程序的目的是什么?因为当我更改月份时,它会自动逐月增加,例如2011年3月3日。。。04/03/2011 05/03/2011 .. 这就是我使用dtpstart的原因。事实上,dtpstart会引发事件,但dtpend在我启动dtpstart之前不会引发。你说的“启动dtpstart”是什么意思?我在回答中添加了。看起来您没有重新连接处理程序。