Home > front end >  how to make a darkmode toggle in form1, reach childform form 2?
how to make a darkmode toggle in form1, reach childform form 2?

Time:04-12

i am making an app with a menubar and a panel to display subforms. I made a darkmode feature that works on the menu and the parantform. (self.backcolor = ...) i chances when i hit a button and runs a method that sets everything to the right colors. but how do i send the code from form 1 (parant) to the child forms?

//form1 (parent)

public bool darkMode = false; public Form activeForm = null; private void OpenChildForm(Form childForm, object btnSender) {

        if (activeForm != null)
        {
            label1.Text = "active form is not nothing";
            
        }
        else
        {
            label1.Text = "active form is nothing";

        }

        activeForm = childForm;
            childForm.TopLevel = false;
            childForm.FormBorderStyle = FormBorderStyle.None;
            childForm.Dock = DockStyle.Fill;
            this.panelDesktopPane.Controls.Add(childForm);
            this.panelDesktopPane.Tag = childForm;
            childForm.BringToFront();
            childForm.Show();
    }

private void button1_Click(object sender, EventArgs e) {

        darkMode = true;
        updateSystemColor();
        updateMenuColors();
    }

public void updateSystemColor() {

        this.BackColor = ColorTranslator.FromHtml(returnSystemColor(darkMode, 0));
        
        panelMenu.BackColor = ColorTranslator.FromHtml(returnSystemColor(darkMode, 1));
        


        button3.ForeColor = ColorTranslator.FromHtml(returnSystemColor(darkMode, 5));
        button4.ForeColor = ColorTranslator.FromHtml(returnSystemColor(darkMode, 5));
        button5.ForeColor = ColorTranslator.FromHtml(returnSystemColor(darkMode, 5));
        button6.ForeColor = ColorTranslator.FromHtml(returnSystemColor(darkMode, 5));
      

    }

    public string returnSystemColor(bool darkmode, int index)
    {
        String[] lightModeColors =
        {
        /*0*/ "#E2E0DF", //lightmode_5 - primary
        /*1*/ "#C4C3C2", //lightmode_4 - secondary
        /*2*/ "#A7A6A5", //lightmode_3
        /*3*/ "#8B8A89", //lightmode_2
        /*4*/ "#706F6E", //lightmode_1

        /*5*/ "#000000", //black

        /*6*/ "#EB4992", //maroon_shades_1 - infoBar
        /*7*/ "#C82175", //maroon_shades_2 - button
        /*8*/ "#A60059", //maroon_shades_3 - logo

        /*9*/ "#D54359", //red_shades_1 - infoBar
        /*10*/ "#B62342", //red_shades_2 - button
        /*11*/ "#97002B", //red_shades_3 - logo

        /*12*/ "#9FE8D8", //green_shades_5 - infoBar
        /*13*/ "#7CC3B4", //green_shades_4 - button
        /*14*/ "#59A092", //green_shades_3 - logo

        /*15*/ "#AED9E6", //blue_shades_5 - infoBar
        /*16*/ "#84AEBA", //blue_shades_4 - button
        /*17*/ "#5B848F", //blue_shades_3 - logo

        /*18*/ "#E8A36A", //yellow_shades_1 - infoBar
        /*19*/ "#BD7D46", //yellow_shades_2 - button
        /*20*/ "#935924", //yellow_shades_3 - logo
        };

        String[] darkModeColors =
        {
        /*0*/ "#231F1B", //darkmode_5 - primary
        /*1*/ "#3D3934", //darkmode_4 - secondary
        /*2*/ "#5A5550", //darkmode_3
        /*3*/ "#78736D", //darkmode_2
        /*4*/ "#97928C", //darkmode_1

        /*5*/ "#FFFFFF", //white

        /*6*/ "#640027", //maroon_shades_5 - infoBar
        /*7*/ "#85003F", //maroon_shades_4 - button
        /*8*/ "#A60059", //maroon_shades_3 - logo

        /*9*/ "#5C0000", //red_shades_5 - infoBar
        /*10*/ "#790017", //red_shades_4 - button
        /*11*/ "#97002B", //red_shades_3 - logo

        /*12*/ "#0C5D51", //green_shades_1 - infoBar
        /*13*/ "#367E71", //green_shades_2 - button
        /*14*/ "#59A092", //green_shades_3 - logo

        /*15*/ "#083842", //blue_shades_1 - infoBar
        /*16*/ "#335D67", //blue_shades_2 - button
        /*17*/ "#5B848F", //blue_shades_3 - logo

         /*18*/ "#461700", //yellow_shades_3 - infoBar
         /*19*/ "#6B3700", //yellow_shades_3 - button
         /*20*/ "#935924", //yellow_shades_3 - logo
        };

        if (darkMode == true)
        {
            return darkModeColors[index];
        }
        else
        {
            return lightModeColors[index];
        }

    }

please help, and i am not very experienced, so an explaination would be greatry appreciated.

CodePudding user response:

Create a public field in childForm, and either set it directly, or add a constructor that does that.

Example:

class ChildForm
{
    //...
    public bool IsDark = false;
}

class ParentForm
{
    ChildForm childForm = new ChildForm();
    childForm.IsDark = true;
    //...
}

Or you can have a method in the child form:

class ChildForm
{
    //...
    public void SetMode(bool isDark)
    {
         //...
    }
}

And then call it from the parent form:

class ParentForm
{
    ChildForm childForm = new ChildForm();
    childForm.SetMode(true);
    //...
}
  • Related