0

i have a windows form named "customer.cs" in which i have a textbox named "phonetxt", i have a class in model named myclass.cs ,i have some lines of code in "customer.cs",

 private void customerphone_Leave(object sender, EventArgs e)
    {
        if (customerphone.Text != "Enter Phone Number" && customerphone.Text == "")
        {
            this.customerphone.Text = "Enter Phone Number";
            this.customerphone.ForeColor = Color.DarkGray;
            errorcustomerphone.Icon = Properties.Resources.err;
            errorcustomerphone.SetError(customerphone, "Enter Phone Number");
        }

        else
        {
            errorcustomerphone.Icon = Properties.Resources.ok;
            errorcustomerphone.SetError(customerphone, "Enter Phone Number");

        }
    }

i want a method that will call in customer.cs

methodfortext("customerphone","errorcustomerphone","Enter PhoneNumber");
 //that will send these parameters to myclass.cs
methodfortext(string controlname, string errorname , string name)
{
    //i want
    controlname.Text = name;
    errorname.Icon = Properties.Resources.err;
}

someone help me please,i'm new in c#.hope u people got what i want.

Rob
  • 26,989
  • 16
  • 82
  • 98
user3368644
  • 79
  • 1
  • 1
  • 5

1 Answers1

0

This can be a good starting point.

using System.Windows.Forms;

public static class ErrorHelper
{
    public static void SetError(Form form, string controlName, string errorProviderName, string errorMessage)
    {
        var textBox = GetControl<TextBox>(form, controlName);
        if (textBox != null)
        {
            textBox.Text = errorMessage;
        }

        // If you create an ErrorProvider control by code
        // do not forget to add it to the form controls collection
        // by calling this.Controls.Add(errorProvider); in the form class
        // otherwise, it will not be found this way.
        var errorProvider = GetControl<ErrorProvider>(form, controlName);
        if (errorProvider != null)
        {
            errorProvider.Icon = Properties.Resources.err;
        }
    }

    private static T GetControl<T>(Form form, string controlName) where T : Control
    {
        foreach (object control in form.Controls)
        {
            var ctl = control as Control;

            if (ctl != null && String.Equals(ctl.Name, controlName))
                return ctl as T;
        }
    }
}
Gabor
  • 3,021
  • 1
  • 11
  • 20