2015-02-05 3 views
0

이 GUI를 작동시키기 위해 다른 접근법을 사용했습니다. 나는 사용자 컨트롤을 사용하고 "픽처 박스"를 반복하여 각 PictureBox에로드 할 별도의 이미지가 필요했습니다. 문제는 이것이 매우 추악한 코드라고 생각하며 더 나은 방법으로 수행 할 수 있습니다. 그러나 이것은 현재 잘 작동합니다. 그래서 누군가가 괜찮은지 또는 어떻게 든 다시 써야한다고 말해 줄 시간이 없다면? 그 그림 상자가 될이foreach 루프에서 반복하기

foreach(Artist artist in artists) 
{ 
    PictureBox pic = new PictureBox(); 
    //Manually set the Size and SizeMode of your pictureBox here 
    pic.ImageLocation = artist.artistPic; 
    flowLayoutPanel.Controls.Add(pic); 
} 
+2

변수에 코드에 값을 추가하지 않는다고 생각합니다. – Seb

+0

'u'가 무엇이든 그 데이터를 다르게 strcture해야합니다. 이미지 당 하나의 속성 대신, 이미지 컬렉션을 유지하거나 각각 이미지가있는 '아티스트'컬렉션을 유지하십시오. –

+0

당신은'foreach'를 필요로하지 않습니다 .. 당신은 이미 자신에 의해'i'를 벌써 증가시키고 있습니다. – scartag

답변

1

artistsforeach을하는 것처럼해야 어디에 당신이 flowLayoutPanel을 만들 수 있습니다에 PictureBox를 만드는 를 대신 따를

foreach(Artist a in artists) 
      { 
       int i = 0; 
       u.picArtistOne.ImageLocation = artists[i].artistPic; 
       i++; 
       u.picArtistTwo.ImageLocation = artists[i].artistPic; 
       i++; 
       u.picArtistThree.ImageLocation = artists[i].artistPic; 
       i++; 
       u.picArtistFour.ImageLocation = artists[i].artistPic; 
       i++; 
       u.picArtistFive.ImageLocation = artists[i].artistPic; 
       i++; 
       u.picArtistSix.ImageLocation = artists[i].artistPic; 
       i++; 
      } 
0

당신은 쓸모 옆에 그것을 할 수 있습니다 이리. 당신이 절대적으로 명명 된 고정 속성 주장 (및 relflect 싶지 않다) 경우

var index = 0; 
Dictionary<int, PictureBox> pictureBoxes = u.Controls 
              .OfType<PictureBox> 
              .ToDictionary(key => index++) 

for(var i = 0; i < index; i++) 
{ 
    pictureBoxes[i].ImageLocation = artists[i].artistPic; 
} 
1

는, 적어도 모든 foreach 문을 제거하고 난 물건 :

u.picArtistOne.ImageLocation = artists[0].artistPic; 
u.picArtistTwo.ImageLocation = artists[1].artistPic; 
u.picArtistThree.ImageLocation = artists[2].artistPic; 
u.picArtistFour.ImageLocation = artists[3].artistPic; 
u.picArtistFive.ImageLocation = artists[4].artistPic; 
u.picArtistSix.ImageLocation = artists[5].artistPic; 
당신은 방금 대신 다음을 수행 고려할 수 있습니다