How can I access a control from a child form

0

I am working on a Windows Forms app. The problem is the following: I have a main form in which I have a panel where I call my forms which are user control.

Starting from there, when I have a form in the panel I call a form (forms) and from that form I call another form (forms) from this last command to make an action in the user contrl in which there is a method that activates a control. My question is how can I navigate to the main form which is the floor of everything and in which there is a panel and within that panel my user control that makes form.

ucProducto uc =Panel1.Controls.OfType<ucProducto>().FirstorDefault();
if(uc != null){
uc.ActivarGrp();

}

I can not find a way to get to the user control. I can not access the main form from the second form form.

    
asked by Pedro Ávila 29.07.2016 в 18:39
source

1 answer

1

If the main form exposes an interface you can access it from the user control just like you would between two forms

When you instantiate the user control when putting it in the Panel you should in the constructor assign the instance of the main form, in this way from the user control you have access to the form that I invoke it and you can perform actions.

It would be the same as what I explain here

Communicate Forms

only that instead of being a form that samples with show () would be a user control that you assign to a panel

ucProducto uc = new ucProducto(this);
//resto codigo
Panel1.Controls.Add(uc);

when you assign the this it would be the main form, which implements the interface, the rest does not change

> > From the second form, I can not access the main form.

If there are many jump forms and you want to get to the root directly, maybe the most neat forms are using events

[Winforms] Singleton - Pass data between forms

In the article I explain the topic, I analyzed the title 2 – Eventos, Informar de cambios entre formulario

    
answered by 29.07.2016 / 19:15
source