如何改变背景图像从另一个形式
本文关键字:图像 另一个 背景 何改变 改变 | 更新日期: 2023-09-27 18:05:26
我有两个形式,我想从第二形式改变第一形式的背景。我已经为form1和form2中的button1选择了背景图像,但没有发生任何事情。提前致谢(Windows表格)形式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();
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中添加一个方法来接收要设置为背景的图像。假设相同的_form1
引用存在于Form2中,您将其添加到Form1:
public void ChangeBGImage(Image bgImage)
{
this.BackgroundImage = bgImage;
}
Form2中,你称之为:
private void button1_Click(Object sender, EventArgs e)
{
_form1.ChangeBGImage(button1.BackgroundImage);
}
try this,
FOrm1
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();
}
}
}
}
Form2
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;
}
}
}
问题是您无法从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;
}
}