Search code examples
c#winformsbuttonbackcolor

c# change button value of parent form from Child


I'm trying to change the back color of a button in parent form from a buttom in child form. Ive set button in parent form to public. no errors but no results.

 void CloserForm2_Click(object sender, EventArgs e)
    {
        Form1 frm = new Form1();
        frm.LobbyBtn.BackColor = Color.Gray;

Solution

  • You are creating a new instance of Form1. You could for example either inject the child form with a reference to the parent form, or use the Application.OpenForms property to retrieve a reference to the already existing instance of the parent form, e.g.:

    void CloserForm2_Click(object sender, EventArgs e)
    {
        Form1 frm = Application.OpenForms.OfType<Form1>().FirstOrDefault();
        frm.LobbyBtn.BackColor = Color.Gray;
    }