2017-01-03 6 views
0
 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); 

コードはWinフォーム(アイコンに注意してください)に次のような出力をレンダリング同じピクチャボックスを表示できません私は1つのアイコンのOUTP取得は、上記の複数回

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

すなわち二回同じアイコンを表示することを望んで同じアイコン、 ut。

one icon

私が間違っているところ誰かが助言していただけますか?ありがとうございました。

[編集] - 以下のコードはまた、唯一1つのアイコン

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); 
+0

方法別=条件のPictureBox 'については、'とそのまま残りは次のようになります。その後、

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 }; } } 

と使用画像ボックスを追加します。このクラスを追加します。 –

答えて

1

あなたは、あなたがそれに=条件を設定するときのように、あなたが同じオブジェクトを参照しているanother = aCollection[289]を設定します。したがって、別の場所を更新するときには、aCollection[289](およびcondition)の場所を変更するとき

2つの画像ボックスを追加するには、2つのオブジェクトインスタンスを作成する必要があります。おそらく、オブジェクトのディープコピーを行いコントロールに追加する拡張メソッドを作成することをお勧めします。

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); 
+0

ジムに感謝、徹底的な解決を感謝! – taylorswiftfan

0

を生成同じピクチャ制御はそれほど新しいものが必要とされ、一度に複数の場所にすることができません。

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)); 
+0

それは問題を解決します、どうもありがとうございます。 コントロールが複数回使用されているとき、VSが何のエラーメッセージも投げていないのはかなり奇妙です。 – taylorswiftfan