其实我是想用C#语言编程,想把图片从Form1的pictureBox1移动到Form2的另一个pictureBox1。
我做了以下操作
在Form1.cs中。
private void button1_Click(object sender, EventArgs e)
{
pictureBox1.Load("C:\\Users\\img_0.bmp");
}
在Form1.Designer中:从私有变为公共。
public System.Windows.Forms.PictureBox pictureBox1;
在Form2.cs中。
private void button1_Click(object sender, EventArgs e)
{
pictureBox1.Image = Form1.pictureBox1.Image;
}
但我得到了这个错误。CS0120: 非静态字段、方法或属性'Form1.pictureBox1'需要一个对象引用。
谁能帮帮我?)
虽然这样做不合适,但是,是可以的。
首先在外部类文件中创建这样的图片框。
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public class Class1
{
public static PictureBox pb1 = new PictureBox();
}
}
然后把它添加到你的第一个窗体中 比如说另一个按钮点击事件会打开另一个窗体。所以,你必须首先删除图片框,这将允许第二个表单使用它。
public Form2()
{
InitializeComponent();
this.Controls.Add(Class1.pb1);
}
private void button2_Click(object sender, EventArgs e)
{
this.Controls.Remove(Class1.pb1);
Form3 f = new Form3();
f.ShowDialog();
this.Controls.Add(Class1.pb1);
}
然后,当你打开下一个窗体的时候,添加图片框,当下一个窗体要关闭的时候,把它删除。
public Form3()
{
InitializeComponent();
this.Controls.Add(Class1.pb1);
this.FormClosing += Form3_FormClosing;
}
private void Form3_FormClosing(object sender, FormClosingEventArgs e)
{
this.Controls.Remove(Class1.pb1);
}
这样,你就可以把图片框从第1个表单移到第2个表单,然后再回到第1个表单。