两种不同形式的两个PictureBox控件 [英] Two PictureBox controls in two different forms

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

问题描述

我有两个具有两种不同形式的图片框。




  • Form1:firstpicturebox

  • form2: picturebox1



我想将firstpicturebox的图像传输到picturebox1。



因此,任何人都可以帮忙并提供相同的解决方案。

解决方案

您实际上是在问两个问题:



1-如何将一个PictureBox的内容放入另一个PictureBox?



2-如何访问控件



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



问题2的答案也不难,但是有很多方法可以做到,而选择一种可能取决于您要对这两种形式进行的处理。 / p>

基本方法始终是获得对另一种形式的有效引用。



这是一种通用方法:




  • 在每种表单中声明另一种表单的类的全局变量。

  • 让这些引用在正确的时刻指向另一种表单

  • 将要访问的所有控件,属性等公开;对于控件,您可以创建一个额外的引用并将其填写,也可以转到设计器并将其范围从私有更改为公共。



什么时候合适?假设在程序启动时创建了form1,并且在form1中通过某些操作创建了form2,则在创建并显示它时,您可以在那里获得对form2的引用:

  form2 =新的Form2(this); 
form2.Show();

这可能是单击按钮,甚至是在form1的加载事件中。



请注意,我已经在构造函数中提供了对 this 的引用!这是将对form1的引用传递到新表单的好方法。因此,form2中的构造函数应如下所示:

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

最后一步是公开您需要访问的控件。转到Designer.cs文件,然后将声明更改为

  private System.Windows.Forms.PictureBox pictureBox1; 

  public System.Windows.Forms.PictureBox pictureBox1; 

完成。



或...



如果您有很多都需要填写的表格访问一个PictureBox,您也可以尝试以下操作:在programm.cs文件中声明一个静态全局引用,如下所示:

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

public static PictureBox thePictureBox;

然后以表格1填写参考

  Program.thePictureBox = pictureBox1; 

现在您也可以用所有其他形式引用它:

  myNextPictureBox42.Image = Program.thePictureBox.Image; 


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.

解决方案

You are really asking two questions in one:

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

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

Question 1 is straightforward: pictureBox1.Image = pictureBox2.Image;

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:

  • Declare global variables of the other form's class in each form.
  • Let these references point to the other form at the right moment!
  • Make any controls, properties etc. you want to access public; for controls you can either create an extra reference and fill it in or you can go to the designer and change its scope from private to public there.

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();

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

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. Therefore the contructor in form2 should look like this:

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

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;

Done.

Or...

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;

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;

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

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