本文介绍了将变量从一个图片框复制到另一个,而不使它们彼此更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将图片框复制到另一个图片框,但我不希望他们彼此更改。

I would like to copy a picturebox to another picturebox, but I do not want them to change with each other.

PictureBox picbox1 = new PictureBox();
PictureBox picbox2 = picbox1;

picbox1.Visible = false; //The problem here is that picbox2.Visible will also become false

c $ c> picbox1 更改而不更改 picbox2 ....

I would like to make picbox1 change without changing picbox2....

我可以这样做吗?

推荐答案

可能最好的方法来维护干净的代码是创建一个扩展方法,您的第二个picturebox目前未添加到控件中。

Probably the best way to maintain clean code is to create an extension method, also note that your second picturebox is currently not added to controls.

public static class MyExtensions
{
    public static PictureBox CreateNewWithAttributes(this PictureBox pb)
    {
        return new PictureBox { Image = pb.Image, Width = pb.Width };
    }
}

Picturebox p2 = p1.CreateNewWithAttributes();
this.Controls.Add(p2);

更多可以在扩展方法上阅读

More can be read on extension methods here

这篇关于将变量从一个图片框复制到另一个,而不使它们彼此更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 17:39