TextChanged希望它在用户输入日期后进行验证,但在用户输入日期时验证

时间:2015-02-16 17:38:13

标签: c# textbox datetimepicker textchanged

嗨,大家这对大多数程序员来说必须是一个简单的逻辑,但我无法弄明白。

我的Windows窗体上有2个datetimepicker datetimepicker 1 = fromdate datetimepicker 2 = todate

从日期到日期我从下面的sql

获得
   SELECT MIN(TransDate) AS mindate, MAX(TransDate) AS maxdate 
FROM dbo.EtimePunchDetail WHERE (EmpID = @empid)

fromdate = mindate和todate = maxdate

fromdate.mindate = mindate  todate.maxdate = maxdate(“因此用户只能使用所选的日期范围”)

我添加了两个带搜索功能的文本框,用户可以输入fromdate和todate进入并检查mindate和maxdate,如果用户输入的日期超出范围,我设置消息框就会抛出错误

我的文本框已更改,即使具有以下查询:

      private void Min_TextChanged(object sender, EventArgs e)
        {

              DateTime date = DateTime.Parse(Min.Text);

              if (date < DateTime.Parse(AvailableMin.Text))
               {
                   MessageBox.Show("The Date you entered is either out of range or an invalid format");
                }
              else
                {
                  FromDate.MinDate = date;
                  FromDate.Value = date;
                }
        }
private void Max_TextChanged(object sender, EventArgs e)
    {
         DateTime date = DateTime.Parse(Max.Text);

         if (date > DateTime.Parse(AvailableMax.Text))
         {
            MessageBox.Show("The Date you entered is either out of range or an invalid format");
         }
         else
         {
            ToDate.MaxDate = date;
            ToDate.Value = date;
         }
         }

但是当我更改文本时,textchanged事件会被消息触发,并且不允许我更改日期,或者说它是无效日期。我希望能够输入一个日期,然后textchanged应该检查输入的日期是否不在范围内我该怎么办?

这是我要求的视觉表现:

enter image description here

更多关于如何获取最小和最大日期的代码以及我正在使用这些值进行的其他事情我应该在我的问题中包含此内容我道歉之前我的事情是datetimepicker验证干扰了文本框验证

最低和最高价值

private void mindateset() // fill the listbox of values of mindate and maxdate
{
if (Employee.SelectedValue != null)
  {
if (Employee.SelectedValue.ToString().Trim().Length > 0)
    {
try
     {
  using (MSSQL.SqlConnection connection = new MSSQL.SqlConnection(constr))
       {
   timepunchnew = new EtimeHistoryDataSet();
    connection.Open();
    using (MSSQL.SqlCommand command = new MSSQL.SqlCommand("SELECT MIN(TransDate) AS mindate, MAX(TransDate) AS maxdate FROM dbo.EtimePunchDetail WHERE (EmpID = @empid)", connection))
    {
   MSSQL.SqlParameter myminparam = new MSSQL.SqlParameter();
   myminparam.Direction = ParameterDirection.Input;
   myminparam.ParameterName = "@empid";
   myminparam.Value = Employee.SelectedValue;
   command.Parameters.Add(myminparam);
   MSSQL.SqlDataAdapter myadapter = new System.Data.SqlClient.SqlDataAdapter();
   myadapter.SelectCommand = command;
   myadapter.Fill(timepunchnew, "Mindate");
   AvailableMin.DataSource = timepunchnew.Mindate;
   AvailableMin.DisplayMember = "mindate";
   AvailableMax.DataSource = timepunchnew.Mindate;
   AvailableMax.DisplayMember = "maxdate";
   FromDate.MinDate = DateTime.Parse(AvailableMin.Text);
   FromDate.Value = FromDate.MinDate;
   ToDate.MaxDate = DateTime.Parse(AvailableMax.Text);
   ToDate.Value = ToDate.MaxDate;
   Min.Text = FromDate.MinDate.ToString("d");
   Max.Text = ToDate.MaxDate.ToString("d");
   }
}
}
catch (Exception) { /*Handle error*/ }

}
}

 }

对datetimepicker值进行验证

private void FromDate_ValueChanged_1(object sender, EventArgs e)
          {
         if (empchanging == false)
         {
          if (FromDate.Value > ToDate.Value)
           {
             // MessageBox.Show("From Date Cannot Be Greater Than To Date");
                 if (DialogResult.OK == MessageBox.Show("From Date Cannot Be Greater Than To Date"))
                 {
                  FromDate.MinDate = DateTime.Parse(AvailableMin.Text);
                  FromDate.Value = FromDate.MinDate;

                }
              }
          }
         }
      private void ToDate_ValueChanged_1(object sender, EventArgs e)
       {
       if (empchanging == false)
       {
       if (ToDate.Value < FromDate.Value)
       {
      //MessageBox.Show("To Date Cannot Be Less Than From Date");
        if (DialogResult.OK == MessageBox.Show("To Date Cannot Be Less Than From Date"))
         {
         ToDate.MaxDate = DateTime.Parse(AvailableMax.Text);
         ToDate.Value = ToDate.MaxDate;
        }
     }
    }
     }

验证空字符串的可用日期范围

private void AvailableMin_SelectedIndexChanged(object sender, EventArgs e)
{
 if (AvailableMin.Text == string.Empty)
  {
   textBox2.Visible = true;
  textBox2.Text = "There is no From Date available for this particular user";

}
else
{
 textBox2.Visible = false;
 }
    }

private void AvailableMax_SelectedIndexChanged(object sender, EventArgs e)
  {
    if (AvailableMax.Text == string.Empty)
    {
      textBox1.Visible = true;
   textBox1.Text = "There is no To Date available for this particular user";
  }
  else
    {
   textBox1.Visible = false;
    }
    }

我尝试了以下解决方案

 private void Min_TextChanged(object sender, EventArgs e)
 {
  DateTime date; 
if (!DateTime.TryParse(Min.Text, out date))
       {
formerrorprovider.SetError(this.Min,"The Date you entered is in invalid format");
  }
   else if (date < DateTime.Parse(AvailableMin.Text))
 {
  formerrorprovider.SetError(this.Min, "The Date you entered is either out of range");
     }
    else 
   {
   formerrorprovider.SetError(this.Min, string.Empty);
    FromDate.MinDate = date;
    FromDate.Value = date;
     }

它接受我输入的日期但是干扰了datetimepicker验证。

enter image description here PLZ帮助

2 个答案:

答案 0 :(得分:2)

作为可能的UI解决方案:如果日期无效,则不显示MessageBox但是向Form添加ErrorProvider控件并将错误描述设置为Max和Min文本框。如果存在不正确的数据,错误提供程序将显示错误图标,并在输入可接受时将其隐藏

private void Max_TextChanged(object sender, EventArgs e)
{
     DateTime date;

     if (!DateTime.TryParse(Max.Text, out date))
     {
        formErrorProvider.SetError(this.Max, "The Date you entered is in invalid format");
     }
     else if (date > DateTime.Parse(AvailableMax.Text))
     {
        formErrorProvider.SetError(this.Max, "The Date you entered is out of range");
     }
     else
     {
        formErrorProvider.SetError(this.Max, string.Empty);
        ToDate.MaxDate = date;
        ToDate.Value = date;
     }
}

答案 1 :(得分:2)

您的代码完全按照您的要求执行:它在文本更改时验证值

如果这不是你想要的行为,那就不要那样做了!

例如,您可能希望验证LeaveLostFocus事件的值。或者,您可以利用Control类中的现有验证模型,并将验证放在一个方法中,该方法是Validating事件的事件处理程序(在Leave和{LostFocus之间引发{1}}事件)。

另一个建议 - 以较少破坏性的方式呈现用户反馈 - 也是一个非常好的建议。这通常会更加用户友好,可能会使用户的反馈更加微妙和容易被用户忽视。