Halfpint
Halfpint

Reputation: 4077

C# ImageList won't display images

I've been trying to figure out why my imageList won't render out my images when my form runs, I am using the following code...

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 + ".");
        }

    }

The method is fired when the user changes the index of my combo box, I know the program gets the correct path as I used a message box to display the path each path returned as I expected it to.

Am I missing something from my code to draw the image to the box?

Alex.

Upvotes: 4

Views: 5656

Answers (1)

Pablo Romeo
Pablo Romeo

Reputation: 11406

After adding all images to the ImageList, you should add all the items to the ListView as well:

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

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

You can also use a FlowLayoutPanel and dynamically create PictureBox elements, one for each Image, and not use ImageLists and ListViews at all. It depends on the type of UI you want.

Upvotes: 4

Related Questions