Question

In my UserControl i have a TextBox Which is not inheriting from TextBox, i am doing a

validation for Numerics (which allows only Digits)

public delegate void usercontrolError(string message);
    public event usercontrolError onerror;

 Private void txtLocl_KeyPress(object sender, KeyPressEventArgs e)
{
    if(e.KeyChar != '\b')
    {
        if(!char.IsDigit(e.KeyChar))
        {
        e.Handled=true;
        }
        else
       {
         onerror.Invoke("Enter Digits Only");
       }
    }
}

in Form i write the following code

 public Form1()
 {
     txtLocl.onerror += new ciscontrols.mtbDtTmPk.usercontrolError(mtbDtTmPk1_onerror);
 }

    void mtbDtTmPk1_onerror(string message)
    {
        epfrm2.SetError(mtbDtTmPk1, message);
        //throw new NotImplementedException();
    }

i write the code in Form. now i don't want to write any code in Form. But errorProvider is in Form1 only. How can i do Know. User cann't write any code in Form1.But Form ErrorProvider will work.

Was it helpful?

Solution

You can fire a event:

public delegate void CustomTextBoxError(string message);
public event CustomTextBoxError onError;

private void txtLocl_KeyPress(object sender, KeyPressEventArgs e)
{
    if(e.KeyChar != '\b')
    {
        if(char.IsDigit(e.KeyChar))
        {
            e.Handled=true;
        }
        else
        {
             if (onError != null)
               onError.Invoke("Enter digits only");
        }
    }
}

And in your form:

private void MyForm_Load(object sender, EventArgs e)
{
   myUserControl.onError += new CustomTextBoxError(MyForm_onError);
}

void MyForm_onError(string message)
{
    //do anything you want in your form...
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top