2011-03-27 65 views
1

其实,我有我的形式2 dateTimePicker在C尖锐净2008年。他们都执行相同的事件。但其中一个是不正常工作,直到我使用另一个one.Please帮我出去! !DateTimePicker不能正常工作

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(); 
    } 
+0

winform,wpf或web? – 2011-03-27 11:32:30

+0

删除处理程序的目的是什么? – Anuraj 2011-03-29 05:52:04

+0

,因为当我改变月,它会自动增加一个月,如03/03/2011 ... 04/03/2011 05/03/2011 ..这就是为什么我使用。 – 2011-03-29 06:01:55

回答

1

检查两个组件的AutoPostBack属性。

+0

我需要在Windows应用程序不在web应用程序 – 2011-03-28 05:20:32

+0

@Prakash,然后请带上一些代码 – 2011-03-28 05:23:16

1

你的代码中有一些非常奇怪的构造。例如,

check = validate(); 
if (check == "Incorrect") 
{ 
    check = "Correct"; 
    return; 
} 

(为什么没有验证()返回一个布尔?为什么你不理验证错误?) 我的建议是进行重组,更名和改变你的方法签名,所以代码变得可读。这会让你更容易发现错误。

您是否使用调试程序逐步完成代码?这个活动是否为两位选手提升?

编辑 您删除处理程序,但从不添加它。所以删除它后,你必须决定何时再次添加它。这不会发生魔法!

+0

实际上,dtpstart获取事件引发,但dtpend不,直到,除非我启动dtpstart。 – 2011-03-29 06:00:31

+0

“start dtpstart”是什么意思? – 2011-03-29 06:07:56

+0

我添加到我的答案。它看起来像你不重新附加处理程序。 – 2011-03-29 06:11:20