I used from Error Provider in C# winform. in my form have textbox. error provider checked it that it contain two number. it means that input is digit and number of digit is two number. when input is 2 char , error provider is worked but when input is char and digit, error provider didn’t worked.
please check my code.
private void textbox1_Leave(object sender, EventArgs e)
{
string text = textbox1.Text;
bool hasDigit = false;
foreach (char letter in text)
{
if (char.IsDigit(letter))
{
hasDigit = true;
break;
}
}
// Call SetError or Clear on the ErrorProvider.
if (!hasDigit )
{
errorProvider1.SetError(textbox1, "Please enter digit");
}
else if(hasDigit)
{
if (text.TextLength != 2)
{
errorProvider1.SetError(textbox1, "Number of digit is two number");
}
else
errorProvider1.Clear();
}
}
So you want to ensure that all chars are digits. But you’re checking only the first, if that’s a digit you’re breaking the loop:
Instead you could use Linq for this.
Enumerable.Allis made for this purpose:(but maybe i’m totally off the track since the question is not so clear imho)