如何在表格 1 中切换暗模式,到达子表格表格 2?

how to make a darkmode toggle in form1, reach childform form 2?

我正在制作一个带有菜单栏和面板的应用程序来显示子表单。我制作了一个适用于菜单和 parantform 的暗模式功能。 (self.backcolor = ...) 我有机会点击一个按钮并运行一种将所有内容设置为正确颜色的方法。但是我如何将代码从表单 1(parant)发送到子表单?

//form1(父级)

public bool darkMode = false; public 表格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];
        }

    }

请帮忙,我不是很有经验,所以非常感谢解释。

在 childForm 中创建一个 public 字段,然后直接设置它,或者添加一个构造函数来执行此操作。

示例:

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

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

或者您可以在子表单中使用一个方法:

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

然后从父表单中调用它:

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