C# 如何从另一个窗体更改背景图像?

C# 如何从另一个窗体更改背景图像?,c#,winforms,forms,C#,Winforms,Forms,我有两张表格,我想把第一张表格的背景从第二张表格改过来。我已经为form1和form2中的button1选择了背景图像,但什么也没发生。thanx预付款(Windows窗体) 第一份表格: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text;

我有两张表格,我想把第一张表格的背景从第二张表格改过来。我已经为form1和form2中的button1选择了背景图像,但什么也没发生。thanx预付款(Windows窗体) 第一份表格:

using System;
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;
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.BackgroundImage = button1.BackgroundImage;
        }
    }
}

在第二个表单中,添加一个私人成员,该成员将持有对第一个表单的引用:

private Form _form1 = null;
然后在Form2的构造函数中,允许传入该引用:

public Form2(Form form1)
{
    InitializeComponent();
    _form1 = form1;
}
现在,在该按钮单击处理程序中,您可以:

private void button1_Click(Object sender, EventArgs e)
{
    _form1.BackgroundImage = button1.BackgroundImage;
}
另一种方法是在Form1中添加一个方法,该方法接收要设置为背景的图像。假设相同的
\u form1
引用存在于Form2中,则将其添加到form1中:

public void ChangeBGImage(Image bgImage)
{
    this.BackgroundImage = bgImage;
}
从表2中,你称之为:

private void button1_Click(Object sender, EventArgs e)
{
    _form1.ChangeBGImage(button1.BackgroundImage);
}

问题是您无法从
form2
访问
form1
来更改它。如果要更改
form1
中的某些内容,则不应创建
form1
的新实例。您应该在构造函数中获取实例

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

    private void button1_Click(object sender, EventArgs e)
    {
        Form2 frm2 = new Form2(this);
        frm2.ShowDialog();
    }
}
public partial class Form2 : Form
{
    Form1 frm1; 
    public Form2(Form1 frm1)
    {
        InitializeComponent();
        this.frm1 = frm1; 
    }

    private void button1_Click(object sender, EventArgs e)
    {
        frm1.BackgroundImage = button1.BackgroundImage;
    }
}
试试这个

表格1

表格2


我应该将什么作为参数发送到Form2 frm2=new Form2();?我想你是在Form1中创建Form2的,对吗?如果是:
表格2 frm2=新表格2(本)
using System;
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();

            //check if button1 clicked and then change the background
            if(frm2.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                  this.BackgroundImage = frm2.GetBackImage();
            }
        }
    }
}
using System;
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.BackgroundImage = button1.BackgroundImage;
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }

       public Image GetBackImage()
       {
           return this.button1.BackgroundImage;
       }
    }
}