Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm making a Windows form which allows people to select a film from different genres. Now i've used list boxes to display the list of films and genres. When a user selects a genre, the films from that genre are displayed in another list box. Now what I want is for a picture to be displayed in a picture box when the user selects a film from the listbox. I've done this for displaying film information, but im not sure how to do this for pictures. Also is it possbile to keep both the film info and the picture selection code in one listbox?

Heres the code for my listbox which contains the films:

C#
private void lbxFilms_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ta == "Action/Thriller")
            {

                switch (lbxFilms.SelectedItem.ToString())
                {
                    case "Casino Royale":
                        lbxInfo.Items.Clear();
                        lbxInfo.Items.Add("Directed By Martin Campbell");
                        lbxInfo.Items.Add("Starring Daniel Craig, Eva Green, Mads Mikkelson");
                        lbxInfo.Items.Add("Run Time 144 Minutes, Special Features: Making Of, Commentary, Deleted Scenes and many more" );
                        break;

                    case "Die Hard":
                        lbxInfo.Items.Clear();
                        lbxInfo.Items.Add("Directed by John McTiernan");
                        lbxInfo.Items.Add("Starring Bruce Willis, Alan Rickman, Bonnie Bedelia");
                        lbxInfo.Items.Add("Run Time 131 minutes, Special Features: Making of, Outakes, deleted scenes and many more");
                        break;

                    case "Raiders of the Lost Ark":
                        lbxInfo.Items.Clear();
                        lbxInfo.Items.Add("Directed by Steven Speilberg");
                        lbxInfo.Items.Add("Starring Harrison Ford, Karen Allen, Paul Freeman");
                        lbxInfo.Items.Add("Run time 115 minutes, Special Features: Making of, StoryBoard Comparison, deleted scenes");
                        break;

                    default:
                        break;
                }

            }
Posted

All you have to do is set the PictureBox.Image property:
C#
if (myPicture.Image != null)
    {
    myPicture.Image.Dispose();
    }
myPicture.Image = Image.FromFile(@"F:\Temp\MyImage.jpg");

The dispose just tidies up after yourself!
You can use any image source you have: it could be a Bitmap you have loaded, or a file as I have shown.


"Is there a way to place that code inside my swicth case. So for example if a select Casino Royale that code will then run along with the info code?"


Yes: just add a line before each break:
C#
private void lbxFilms_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ta == "Action/Thriller")
            {
                switch (lbxFilms.SelectedItem.ToString())
                {
                    case "Casino Royale":
                        lbxInfo.Items.Clear();
                        lbxInfo.Items.Add("Directed By Martin Campbell");
                        lbxInfo.Items.Add("Starring Daniel Craig, Eva Green, Mads Mikkelson");
                        lbxInfo.Items.Add("Run Time 144 Minutes, Special Features: Making Of, Commentary, Deleted Scenes and many more" );
                        SetPicture(@"F:\Films\Casino Royale.jpg");
                        break;
                    case "Die Hard":
                        lbxInfo.Items.Clear();
                        lbxInfo.Items.Add("Directed by John McTiernan");
                        lbxInfo.Items.Add("Starring Bruce Willis, Alan Rickman, Bonnie Bedelia");
                        lbxInfo.Items.Add("Run Time 131 minutes, Special Features: Making of, Outakes, deleted scenes and many more");
                        SetPicture(@"F:\Films\Die Hard.jpg");
                        break;
                    case "Raiders of the Lost Ark":
                        lbxInfo.Items.Clear();
                        lbxInfo.Items.Add("Directed by Steven Speilberg");
                        lbxInfo.Items.Add("Starring Harrison Ford, Karen Allen, Paul Freeman");
                        lbxInfo.Items.Add("Run time 115 minutes, Special Features: Making of, StoryBoard Comparison, deleted scenes");
                        SetPicture(@"F:\Films\Raiders of the Lost Ark.jpg");
                        break;
                    default:
                        SetPicture(@"F:\Films\None.jpg");
                        break;
                }
            }
And add the code I gave you as a method:
C#
private void SetPicture(string imagePath)
    {
    if (myPicture.Image != null)
        {
        myPicture.Image.Dispose();
        }
    myPicture.Image = Image.FromFile(imagePath);
    }
 
Share this answer
 
v2
Comments
programmer1234 16-Feb-11 7:25am    
Is there a way to place that code inside my swicth case. So for example if a select Casino Royale that code will then run along with the info code?
OriginalGriff 16-Feb-11 7:31am    
Answer updated
programmer1234 16-Feb-11 7:35am    
Ok that works, thanks for your help
Olivier Levrey 16-Feb-11 7:56am    
Then you should accept it as an answer (press the "accept answer" button), otherwise people will think you are still searching for an answer.
JF2015 17-Feb-11 1:00am    
Helpful answer.
This may be of interest:
Extending the ListBox to show more complex items[^] - provides a listbox that allows you to display an image to the right of the text.

Regards
Espen Harlinn
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 16-Feb-11 16:20pm    
Should work, my 5.
--SA
Espen Harlinn 16-Feb-11 17:57pm    
Thank you, SAKryukov!
All you really need is to create a special class/structure used as a list item; for presentation, override object.ToString.

For my code sample, please see my Answer to this Question:

Displaying an image from a list box[^]

Even though this code is shown for WPF, it will look nearly the same of System.Windows.Forms.

—SA
 
Share this answer
 
v2
Comments
Espen Harlinn 16-Feb-11 14:42pm    
My 5 - wpf is obviously better suited for this :)
Sergey Alexandrovich Kryukov 16-Feb-11 15:29pm    
Thank you for the note, I added to the Answer that it will look nearly the same for Forms. It wasn't obvious what OP uses as there is not tag about UI library used.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900