我有两个表单,我想从第二个表单更改第一个表单的backGround。我已经在form2中为form1和button1选择了backGround图像,但没有任何反应。提前(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;
}
}
}
答案 0 :(得分:0)
在第二种形式中,添加一个私有成员,该成员将保留对第一个表单的引用:
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添加一个接收要设置为背景的图像的方法。假设Form2中存在相同的_form1
引用,您将其添加到Form1:
public void ChangeBGImage(Image bgImage)
{
this.BackgroundImage = bgImage;
}
从Form2中,您可以称之为:
private void button1_Click(Object sender, EventArgs e)
{
_form1.ChangeBGImage(button1.BackgroundImage);
}
答案 1 :(得分:0)
问题是您无法从form1
访问form2
进行更改。如果您想更改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;
}
}
答案 2 :(得分:0)
试试这个,
FOrm 1
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();
}
}
}
}
表格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 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;
}
}
}