2013-04-09 53 views
2

我一直在試圖找出爲什麼當我的窗體運行,我使用下面的代碼我的圖像列表將不會呈現出我的圖片...C#的ImageList也不會顯示圖像

public void renderImageList() 
    { 
     int selection = cboSelectedLeague.SelectedIndex; 
     League whichLeague = (League)frmMainMenu.allLeagues[selection]; 


     string index = cboSelectedLeague.SelectedItem.ToString(); 

     if (whichLeague.getLeagueName() == index) 
     { 
      foreach (Team t in allTeams) 
      { 
       Image teamIcon = Image.FromFile(@"../logos/" + t.getTeamLogo()); 

       imgLstIcons.Images.Add(teamIcon); 

      } 

     } 

     else 
     { 
      MessageBox.Show("Something went wrong..." + whichLeague.getLeagueName() + " " + index + "."); 
     } 

    } 

的方法是當用戶更改我的組合框的索引時觸發的,我知道該程序獲得了正確的路徑,因爲我使用了一個消息框來顯示每個路徑按照我的預期返回的路徑。

我是否錯過了我的代碼將圖像繪製到框中?

Alex。

+1

你不應該使用PictureBox嗎? ImageLists不用於直接在表單上顯示圖像,而是用作其他控件使用的一包資源。 http://stackoverflow.com/a/8587685/1373170 – 2013-04-09 16:49:33

+0

將圖像繪製到哪個框?您可以從我所看到的內容中添加到「ImageList」中。這不顯示圖像... – MoonKnight 2013-04-09 16:49:35

+0

啊好吧,imageList被設置爲我的listView頂部的largeImage控件 - 我需要將他們繪製到listView? – Alex 2013-04-09 16:51:26

回答

2

將所有圖像到ImageList後,你應該所有項目添加到ListView還有:

for (int j = 0; j < imgLstIcons.Images.Count; j++) 
{ 
    ListViewItem item = new ListViewItem(); 
    item.ImageIndex = j; 
    lstView.Items.Add(item); 
} 

http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/876b6517-7306-44b0-88df-caebf3b1c10f/

你也可以使用一個FlowLayoutPanel的和動態創建的PictureBox元素,每個圖像一個,並且根本不使用ImageLists和ListViews。這取決於你想要的UI的類型。

+0

FlowLayoutPanel實際上聽起來很有趣,我可以試試看,謝謝! – Alex 2013-04-10 20:57:31