I have the following user control being used in a Form:
public partial class CampoTelefone : UserControl
{
private void maskedTextBoxTelefone_Validating(object sender, CancelEventArgs e)
{
//validações
}
}
After the user populate the user control in the Form I pass it to another class that performs other types of validations, the parameter container
is receiving 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 instance of user control to the method that executes foreach
. The properties I can access are only those I set at the time of user control implementation.
In this part of the code using the control TextBox
it works correctly:
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 exclude the code part of the checks by question space, and I used campoTelefone
to simplify.
What would be the solution to this problem?