Allowing blank values for validated textbox

  • Thread starter Thread starter Ken Loomis
  • Start date Start date
K

Ken Loomis

Hello:

I'm using the validating event on a text boxes to test for valid
dates, doubles, etc. The problem is that if the field is not required
and a blank value is OK and the user deletes a previous non-blank
value, the validaing event keeps putting the non-blank value back in
the text box and doesn't allow the blank value. Is this unavoidable
in the built-in validating event and do I need just to write my own
validation event?

Thanks,

Ken
 
Ken said:
Hello:

I'm using the validating event on a text boxes to test for valid
dates, doubles, etc. The problem is that if the field is not required
and a blank value is OK and the user deletes a previous non-blank
value, the validaing event keeps putting the non-blank value back in
the text box and doesn't allow the blank value. Is this unavoidable
in the built-in validating event and do I need just to write my own
validation event?

Thanks,

Ken

The validation should just verify the value, not write anything.
Are you sure you don't put it back yourself by accident?
Or that you skip blank field when you store the contents,
so the non-blank value will not be overwritten?

Do you have some code to show?

Hans Kesting
 
Ken Loomis wrote:

The validation should just verify the value, not write anything.
Are you sure you don't put it back yourself by accident?
Or that you skip blank field when you store the contents,
so the non-blank value will not be overwritten?

Hans, thanks for your reply. If I comment out the validating event,
the blank value sticks. That's what made me think it was the
validation that was causing it.

This is what I have for validating:

private void txtHours_Validating(object sender,
System.ComponentModel.CancelEventArgs e)
{
if (txtHours.Text.Length != 0 )
{
try
{
double hours = Convert.ToDouble(txtHours.Text);
}
catch
{
this.errorProvider1.SetError(txtHours,"Enter hours in 1.00
format");
e.Cancel = true;
}
}
}

And here's what I have for validated:

private void txtHours_Validated(object sender, System.EventArgs e)
{
this.errorProvider1.SetError(txtHours,"");
}

Ken
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top