User Control C#

Asked

Viewed 421 times

4

I have the following user control that is being used in a Form:

public partial class CampoTelefone : UserControl 
{
    private void maskedTextBoxTelefone_Validating(object sender, CancelEventArgs e) 
    {
        //validações 
    }         
}

After the user fills the user control in the Form I pass it to another class that performs other types of validations, the parameter container is getting a GroupBox:

private void valida(Control container)
{
    foreach (Control c in container.Controls.Cast<Control>().OrderBy(c => c.TabIndex))
    {
        valida(c);

        if (c is CampoTelefone && c.Tag.ToString() == "1") 
        { 
            /*outras validações*/ 
        }          
    }
}

In this part if (c is CampoTelefone && c.Tag.ToString() == "1") I need to access the properties of c which in this case is my user control, but its properties are not accessible, even passing a new user control instance to the method that runs the foreach. The properties I can access are only those I defined at the time of user control implementation.

In this part of the code using control TextBox works properly:

foreach (Control c in container.Controls.Cast<Control>().OrderBy(c => c.TabIndex))
{
    valida(c);
    if (c is TextBox && c.Tag.ToString() == "1")
    {
        // faço mais validacoes
    }
}

I excluded the part of the code from the checks for the sake of space of the question, and used the campoTelefone to simplify.

What would be the solution to this problem?

  • The paragraph where you say O problema é o seguinte... got very confused. If you can [Edit] your question and explain better, I’m sure I can help you with that =).

  • Okay, I’m gonna make this part better.

  • @jbueno, see if it’s clearer.

  • I think I got your problem, but I don’t understand what you mean by As propriedades que consigo acessar são somente as que defini no momento da implementação do user control. Give me an example of these properties.

  • I meant control properties, example: Tag, Mask, Textalign, Locked, Tabstop.

1 answer

4


Turns out you’re asking for an instance of Control in its method, even if its object is of a type derived from it the received instance will be of Control.

You just need to convert this object to the type you want, see (I’ll give you two conversion examples, choose the most suitable):

private void valida(Control container)
{
    foreach (Control c in container.Controls.Cast<Control>().OrderBy(c => c.TabIndex))
    {
        c = (CampoTelefone)c; //estoura uma exception se não for possível converter
        c = c as CampoTelefone; // c recebe null se não for possível converter

        valida(c);

        if (c is CampoTelefone && c.Tag.ToString() == "1") 
        { 
            /*outras validações*/ 
        }          
    }
}

This question may help you to understand the is: Difference between the use of typeof and is

  • Thanks, now it is recognizing the type correctly with typeof, however the properties are still with error.

  • You have to do the conversion, see there c = (CampoTelefone)c;.

  • Solved. Thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.