两个图片框在两种不同的形式 [英] Two Picturebox in two different forms

查看:148
本文介绍了两个图片框在两种不同的形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在两个不同的形成两个pictureboxes。
Form1中:firstpicturebox
窗口2:picturebox1
我想firstpicturebox的图像传送到picturebox1。
所以,谁能帮助,并提供相同的解决方案。
在此先感谢

I have two pictureboxes in two different forms. Form1 : firstpicturebox form2 : picturebox1 I want to transfer the image of firstpicturebox to picturebox1. So, can anyone help and provide the solution for the same. Thanks in advance.

推荐答案

您真的问两个问题一:

1 - 我怎样才能得到一个图片框的内容到另一个图片框

1 - How can I get the content of one PictureBox into another PictureBox?

2 - ?我怎样才能访问控件等一种形式的?从另一种形式

2 - How can I access the controls etc. of one form from another form?

1的问题很简单: pictureBox1.Image = pictureBox2.Image;

第2题的答案是不难要么但也有很多方法可以做到这一点,选择一个可能取决于你想用这两种形式做什么。

The answer to question 2 is not hard either but there are many ways to do it and selecting one may depend on what else you want to do with the two forms.

的基本方法是始终得到其他形式的有效参考

The basic way is always to get a valid reference to the other form.

下面是一个通用的方法:

Here is a general purpose method:


  • 声明其它形式的类的全局变量在每个表单中。

  • 让这些引用指向其他形式的< STRONG>在适当的时候

  • 请任何控件,属性等你要访问的公共!;控件您可以创建一个额外的引用和填充它,或者你可以去设计和更改私人其范围为公共那里。

什么是合适的时机?假设Form1将在PROGRAMM的启动创建,创建窗体2通过在Form1一些行动,你可以得到参考FORM2在那里,当你创建并显示:

What is the right moment? Assuming form1 is created at the programm's startup and form2 is created by some action in form1 you could get the reference to form2 right there when you create and show it:

    form2 = new Form2(this);
    form2.Show();

这可能是一个按钮的点击,甚至在Form1的Load事件。

This could be in a button click or even in the load event of form1.

请注意,我在构造函数中递了参考这个!这是对引用到Form1传递给新形式的好方法。为此在窗口2的构造器应该是这样的:

Note that I have handed a reference to this in the constructor! This is a nice way to pass the reference to form1 to the new form. Therefor the contructor in form2 should look like this:

public Form2(Form1 form1_)
{
    InitializeComponent();
    form1 = form1_;
}



最后一步是使控制公共您需要访问。进入Designer.cs文件并更改从

The last step is to make the controls public which you need to access. Go to the Designer.cs file and change the declaration from

private System.Windows.Forms.PictureBox pictureBox1;



to

public System.Windows.Forms.PictureBox pictureBox1;

完成。

或...

如果您有很多形式,它们都需要访问一个图片框,你也可以试试这个:声明静态全局引用它在programm.cs文件是这样的:

If you have many forms which all need to access the one PictureBox, you could also try this: Declare a static global reference to it in the programm.cs file like this:

static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new Form1());
}

public static PictureBox thePictureBox;



然后填写在Form1

Then fill in the reference in the form1

    Program.thePictureBox = pictureBox1;

现在,你可以参考它在所有其他形式,以及:

Now you can reference it in all other forms as well:

myNextPictureBox42.Image = Program.thePictureBox.Image;

这篇关于两个图片框在两种不同的形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆