【问题标题】:Can't display the same PictureBox more than once不能多次显示同一个 PictureBox
【发布时间】:2017-01-03 01:37:43
【问题描述】:
        Dictionary<int, PictureBox> aCollection;

        aCollection = new Dictionary<int, PictureBox>();

        aCollection.Add(333, new PictureBox
            {
                Name = "Alcoholism",
                Image = Resources.alcoholism,
                Size = new Size(22, 22),
                SizeMode = PictureBoxSizeMode.StretchImage
        });

        aCollection.Add(289, new PictureBox
        {
            Name = "Hypertension",
            Image = Resources.hypertension,
            Size = new Size(22, 22),
            SizeMode = PictureBoxSizeMode.StretchImage
        });

        PictureBox condition = aCollection[333]; //333 refers to alcoholism
        condition.Location = new Point(450, 155);
        displayForm.Controls.Add(condition);

        PictureBox another = aCollection[289]; //289 refers to hypertension
        another.Location = new Point(550, 155);
        displayForm.Controls.Add(another);

上面的代码在 Winform 上呈现以下输出(注意图标):

但是,如果我将两个 PictureBox 切换为使用相同的图标,希望显示相同的图标两次,即

        PictureBox condition = aCollection[289]; //Hypertension
        condition.Location = new Point(450, 155);
        displayForm.Controls.Add(condition);

        PictureBox another = aCollection[289]; //Hypertension
        another.Location = new Point(550, 155);
        displayForm.Controls.Add(another);

我只得到一个图标输出。

有人可以告诉我哪里出错了吗?谢谢。

[编辑] - 下面的代码也只产生一个图标

    PictureBox condition = aCollection[289];
    condition.Location = new Point(450, 155);
    displayForm.Controls.Add(condition);

    PictureBox another = condition;
    another.Location = new Point(550, 155);
    displayForm.Controls.Add(another);

【问题讨论】:

  • PictureBox another = condition; 怎么样,休息就这样吧。

标签: c# winforms picturebox


【解决方案1】:

当您设置another = aCollection[289] 时,您引用的对象与您设置条件 = 时所引用的对象相同。因此,当您更新另一个位置时,您正在更改aCollection[289](以及condition)的位置

您需要创建 2 个单独的对象实例来添加 2 个图片框。可能最好制作一个扩展方法来对对象进行深层复制,然后将它们添加到控件中。添加这个类:

public static class MyExtension
{
    public static PictureBox DeepCopy(this PictureBox pb)
    {
        return new PictureBox { Name = pb.Name, Image = pb.Image, Size = pb.Size, SizeMode = pb.SizeMode };
    }
}

然后使用以下命令添加图片框:

    PictureBox condition = aCollection[289].DeepCopy(); //289 refers to hypertension
    condition.Location = new Point(450, 155);
    this.Controls.Add(condition);

    PictureBox another = aCollection[289].DeepCopy(); //289 refers to hypertension
    another.Location = new Point(550, 155);
    this.Controls.Add(another);

【讨论】:

  • 感谢 Jim,感谢您的彻底解决方案!
【解决方案2】:

同一个 PictureBox 控件不能同时位于多个位置,因此需要一个新的。

PictureBox newPictureBox(Image image, int X, int Y) {
    return new PictureBox() {
        Image = image,
        Size = new Size(22, 22),
        Location = new Point(X, Y),
        SizeMode = PictureBoxSizeMode.StretchImage
    };
}

然后

displayForm.Controls.Add(newPictureBox(Resources.hypertension, 450, 155));
displayForm.Controls.Add(newPictureBox(Resources.hypertension, 550, 155));

【讨论】:

  • 这样就解决了问题,非常感谢。虽然很奇怪,当控件被多次使用时,VS 根本没有抛出任何错误消息。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多