0

I'm trying to open a Panel that is in the main form (form1) from a button that is inside a UserControl , but the code runs but does not enable the panel of the main form Can you help me?

//UserControl code
private void BtnChangeStatusOrder_Click(object sender, EventArgs e)
    {
        Button seta = (Button)sender;
        var form = new Form1();
        form.EnabledPanel1(seta.Tag.ToString());
    }

//main form code
    public void EnabledPanel(string order)
        {
          
            panel1.Visible = true;
        }
Leonardo
  • 23
  • 4
  • _open a panel on the main form, but the panel only works on the form_ I have no idea what that might mean..?! - `var form = new Form1();` usually a mistake: This doesn't give you access to the actual mainform but just creates a new instance, that is not even shown.. - You may want to create a variable `internal f1 Form1` in the UO and set it maybe in the form load. – TaW Mar 21 '22 at 23:35
  • Writing `var form = new Form1();` means you're creating a new form. You need to keep a reference to the existing main form and use that. – Enigmativity Mar 22 '22 at 00:44
  • 1
    It's the UserControl that needs to raise a public Event when *something happens inside*. Subscribe to this event in your Form1 and when the event is raised, call your method. An example here: [How to know in a Form the name of a Button clicked in a UserControl](https://stackoverflow.com/a/67256385/7444103) – Jimi Mar 22 '22 at 02:29

1 Answers1

0

Assuming the UserControl is also contained by Form1, then you can use TopLevelControl to get a reference to the Form:

private void BtnChangeStatusOrder_Click(object sender, EventArgs e)
{
    Button seta = (Button)sender;
    Form1 f1 = (Form1)this.TopLevelControl;
    f1.EnabledPanel1(seta.Tag.ToString());
}

*Why does EnabledPanel() receive a string?

Idle_Mind
  • 38,363
  • 3
  • 29
  • 40