TextChanged 希望它在用户输入日期后验证,但它在用户输入日期时验证
TextChanged want it to validate after the user has entered a date but it validates as the user is entering the date
大家好,对于大多数程序员来说,这一定是一个简单的逻辑,但我无法理解。
我的 windows 表单上有 2 个日期时间选择器
日期时间选择器 1 = 来自日期
日期时间选择器 2 = todate
我从下面得到的起始日期和截止日期 sql
SELECT MIN(TransDate) AS mindate, MAX(TransDate) AS maxdate
FROM dbo.EtimePunchDetail WHERE (EmpID = @empid)
fromdate = mindate and todate = maxdate
fromdate.mindate = 正念
todate.maxdate = 最大日期 ("So the user is only working with the selected date range")
我添加了两个具有搜索功能的文本框,用户可以在其中输入 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 应该检查输入的日期是否不在范围内我该怎么做??
这是我所要求的视觉表示:
有关我如何获取最小和最大日期以及我正在使用这些值做的其他事情的更多代码我应该在我道歉之前将其包含在我的问题中我认为 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*/ }
}
}
}
日期时间选择器值的验证
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;
}
它接受我输入的日期但干扰了日期时间选择器验证。
PLZ 帮助
作为可能的 UI 解决方案:如果日期无效则不显示 MessageBox,而是将 ErrorProvider 控件添加到 Form 并将错误描述设置为 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;
}
}
您的代码完全按照您的要求执行:它会在文本更改时验证值。
如果这不是您想要的行为,请不要那样做!
例如,您可能更喜欢验证 Leave
or LostFocus
event. Alternatively, you can leverage the existing validation model in the Control
class and put your validation in a method that is an event handler for the Validating
事件(在 Leave
和 LostFocus
事件之间引发)的值。
另一个建议——以较少干扰的方式呈现用户反馈——也是一个很好的建议。总的来说,这将更加用户友好,但可能会以让用户反馈更加微妙和更容易被用户忽视为代价。
大家好,对于大多数程序员来说,这一定是一个简单的逻辑,但我无法理解。
我的 windows 表单上有 2 个日期时间选择器 日期时间选择器 1 = 来自日期 日期时间选择器 2 = todate
我从下面得到的起始日期和截止日期 sql
SELECT MIN(TransDate) AS mindate, MAX(TransDate) AS maxdate
FROM dbo.EtimePunchDetail WHERE (EmpID = @empid)
fromdate = mindate and todate = maxdate
fromdate.mindate = 正念 todate.maxdate = 最大日期 ("So the user is only working with the selected date range")
我添加了两个具有搜索功能的文本框,用户可以在其中输入 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 应该检查输入的日期是否不在范围内我该怎么做??
这是我所要求的视觉表示:
有关我如何获取最小和最大日期以及我正在使用这些值做的其他事情的更多代码我应该在我道歉之前将其包含在我的问题中我认为 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*/ }
}
}
}
日期时间选择器值的验证
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;
}
它接受我输入的日期但干扰了日期时间选择器验证。
作为可能的 UI 解决方案:如果日期无效则不显示 MessageBox,而是将 ErrorProvider 控件添加到 Form 并将错误描述设置为 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;
}
}
您的代码完全按照您的要求执行:它会在文本更改时验证值。
如果这不是您想要的行为,请不要那样做!
例如,您可能更喜欢验证 Leave
or LostFocus
event. Alternatively, you can leverage the existing validation model in the Control
class and put your validation in a method that is an event handler for the Validating
事件(在 Leave
和 LostFocus
事件之间引发)的值。
另一个建议——以较少干扰的方式呈现用户反馈——也是一个很好的建议。总的来说,这将更加用户友好,但可能会以让用户反馈更加微妙和更容易被用户忽视为代价。