如何在另一个表单上实时使按钮更改颜色

时间:2018-09-08 12:40:18

标签: c# winforms

如何从“主题”表单实时更改mainUI(Form)的颜色?我试过简单,但不会实时更新。这是我使用的代码

MainUI:

private void button7_Click(object sender, EventArgs e)
    {
        Themes theme = new Themes();
        theme.Show();
    }

主题:

private void button4_Click(object sender, EventArgs e)
    {
        MainUI n = new MainUI();
        n.BackColor = Color.FromArgb(255, 255, 255);           

    }

1 个答案:

答案 0 :(得分:0)

主题

private MainUI _mainUI;

public Themes(MainUI mainUI)
{
   _mainUI = mainUI
}

private void button4_Click(object sender, EventArgs e)
{
    _mainUI.BackColor = Color.FromArgb(255, 255, 255);           
}

MainUI

private void button7_Click(object sender, EventArgs e)
{
    Themes theme = new Themes(this);
    theme.Show();
}

如果您想访问其他任何内容,例如按钮,则必须将其设置为public。但是,您也可以创建方法来执行相同的操作

MainUI

public void DoOotherStuff()
{
    // are your basses belong to us
}

主题

private void button4_Click(object sender, EventArgs e)
{
    _mainUI.BackColor = Color.FromArgb(255, 255, 255); 
    _mainUI.DoOotherStuff();          
}