Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从第二个窗体更改窗体的背景?_C#_Winforms - Fatal编程技术网

C# 如何从第二个窗体更改窗体的背景?

C# 如何从第二个窗体更改窗体的背景?,c#,winforms,C#,Winforms,我正在使用C#windows窗体,但遇到了一个问题。我有一个表单,我想改变它的背景,但是,我想从第二个表单开始。第二个窗体有一个按钮,按下该按钮时,第一个窗体的背景会发生变化。这是我的密码 第一种形式: using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using

我正在使用C#windows窗体,但遇到了一个问题。我有一个表单,我想改变它的背景,但是,我想从第二个表单开始。第二个窗体有一个按钮,按下该按钮时,第一个窗体的背景会发生变化。这是我的密码 第一种形式:

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 frm2 = new Form2();
            frm2.ShowDialog();
        }
    }
}
第二种形式:

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form1 frm1 = new Form1();
            frm1.TransparencyKey = Color.Turquoise;
            frm1.BackColor = Color.Turquoise;
        }  
    }
}

该按钮应使第一个窗体透明。然而,这是行不通的。我错过什么了吗?谢谢大家!

您可以使用委托和事件或通过在父窗体中实现单例来完成

您可以将表单2的所有者设置为表单1。然后通过这种方式访问它的属性

表格一

private void button1_Click(object sender, EventArgs e)
{
    Form2 frm2 = new Form2();
    frm2.ShowDialog(this);
}
表格二

private void button1_Click(object sender, EventArgs e)
{
    Form1 frm1 = (Form1)this.Owner;
    frm1.TransparencyKey = Color.Turquoise;
    frm1.BackColor = Color.Turquoise;
}  

是:
Form1 frm1=新Form1()
=>
Form1
是一个新的表单实例,而不是创建并打开
Form2
实例的表单实例。因此,您正在更改从未显示过的表单实例的背景色。您需要Form1的当前实例与之交互。您可以找到许多与此主题相关的问答。例如:---也可以使用。