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 biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Converting Error Provider to Control

Status
Not open for further replies.

robertfah

Programmer
Mar 20, 2006
380
US
I'm trying to create a function that sets an Error Provider on my forms from one class, that way if something changes with it, I can change it in one spot, not 20.

Here's my function:
Code:
public void SetError(DevExpress.XtraEditors.XtraForm frm, System.Windows.Forms.Control ctrl, string sErrorMessage, bool IsGroupControlVisible)
{
    DevExpress.XtraEditors.GroupControl gc = (DevExpress.XtraEditors.GroupControl)frm.Controls.Find("gcError", true)[0];
    System.Windows.Forms.Label lbl = (System.Windows.Forms.Label)frm.Controls.Find("lblError", true)[0];
    System.Windows.Forms.ErrorProvider ep = (System.Windows.Forms.ErrorProvider)frm.Controls.Find("epError", true)[0];

    ep.SetError(ctrl, sErrorMessage);

    if (lbl.Text.Length > 0)
        lbl.Text += "\n" + sErrorMessage;
    else
        lbl.Text = sErrorMessage;

    gc.Visible = IsGroupControlVisible;
}
and the statement that's causing the error is:
Code:
System.Windows.Forms.ErrorProvider ep = (System.Windows.Forms.ErrorProvider)frm.Controls.Find("epError", true)[0];

[i]
Cannot implicitly convert type System.Windows.Forms.Control to System.Windows.Forms.ErrorProvider
[/i]

Any ideas?
 
You're trying to pass into System.Windows.Forms.ErrorProvider a type of System.Windows.Forms.Control

Hence error

C
 
Ok, understandably so....how can I fix it? The frm.controls.find returns a type of control, do I need to cast that to a type control first, then cast that to a type error provider?
 
ErrorProvider doesn't follow the same inheritance tree as Control. You're not going to get it this way.

What are you trying to achieve? Using the same error provider on lots of forms? If so, pass it in via the constructor.

C
 
well I have a lot of forms with the same error provider on them....epError. and within each form, I have the above code that sets the error based on the parameters passed in....that works fine, but I'm finding as I get more into this project, the more forms I have that do the same thing (set an error provider). So, instead of having the above code on say 20+ forms and if I wanted to change the something about the error provider (let's say add another line return to make the error message look cleaner), I would have to change that code in all 20+ forms. instead, I wanted to create a class that you could pass in the unknowns (i.e. control to set the error for, error message, etc.) and just have all the forms call this one method. I can pass in the error provider, but it didn't make sense seeing as how every error provider on each form was named the same.

Make any sense?
 
I think so.

So you're not looking to have the same error provider itself but want to be able to set properties.

My soln: Create your own error provider, inheriting from base.

C
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top