Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

winforms errorprovider will not work after programatically changing data values in textboxs

Status
Not open for further replies.

Rousseau10

Programmer
Feb 22, 2005
242
US
Hi all,

WinForms errorProvider will not work after programmatically changing data values in textboxes
I tried ErrorProvider1.Clear() but did not help. If it is a UI change by user, there is no problem with ErrorProvider1.
I tested after program changes code that it still runs through the txtAPU_Validated(object sender, EventArgs e) code correctly.
But no invalid icon next to field(s) appears.

So this code is my 'long way around the barn' work-around,, I had to use a temporary work around using a Boolean variable to change of textbox.text data value was changed in code, then dragged on and using errorProvidor3.

Code:
private void txtAPU_Validated(object sender, EventArgs e)
{
    bool bTest = txtAPUIsValid(@"(?i)^\d[0-9]?.\d{0,3}$");
    if (!progChangedText)//A.R.S. 7-12-15 - bool to check if the program changed the code (not the User)
    {

        if (bTest == true)
        {
            this.errorProvider1.SetError(txtAPU, "Field must contain only numbers and a decimal point. " +
                                                 "Up to 2 numbers before decimal point and up to 3 numbers trailing it.");
        }
        else
        {
            this.errorProvider1.SetError(txtAPU, "");
        }
    }
    else
    {
        if (bTest == true)
        {
            this.errorProvider3.SetError(txtAPU, "Field must contain only numbers and a decimal point. " +
                                                 "Up to 2 numbers before decimal point and up to 3 numbers trailing it.");
        }
        else
        {
            this.errorProvider3.SetError(txtAPU, "");
        }
    }

private bool txtAPUIsValid(string _rgxPattern)
{
    bool testBool = true;
    RgxPattern = _rgxPattern;
    Regex x;
    x = new Regex(RgxPattern, RegexOptions.IgnorePatternWhitespace);

    if (x.IsMatch(txtAPU.Text))
    {
        return false;
    }
    return testBool;
}
}

Here is the code for one of the button controls that repopulates TextBox.Text values that causes ErrorProvider1 to stop working correctly.

Code:
private void btnResetOriginalData_Click(object sender, EventArgs e)
        {
            errorProvider1.Clear(); errorProvider2.Clear(); errorProvider3.Clear();
            int i = 0;
            foreach (Control ctl in this.tlpTextBoxes.Controls)
            {
                if (ctl is TextBox)
                {
                    TextBox textBox = (TextBox)ctl;//casting Control Type to textbox
                    textBox.ResetText();
                    textBox.Text = strListControlValues[i];
                    i++;
                }
            }
        }

Thanks for any Help!
Rousseau10

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top