all groups > c# > july 2005 >
You're in the

c#

group:

Allowing blank values for validated textbox



Allowing blank values for validated textbox Ken Loomis
7/26/2005 11:36:50 PM
c#: 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

Re: Allowing blank values for validated textbox Hans Kesting
7/27/2005 10:17:16 AM
[quoted text, click to view]

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

Re: Allowing blank values for validated textbox Ken Loomis
7/27/2005 3:34:00 PM
On Wed, 27 Jul 2005 10:17:16 +0200, "Hans Kesting"
[quoted text, click to view]

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,"");
}

AddThis Social Bookmark Button