GoGo
GoGo

Reputation: 3057

Showing picture with listbox control in c#

I have a windows form that I design to show pictures. The listbox has All the people's names that come from sql database. So the idea is, when the user click on the name in the listbox, the corresponded picture needs to be displayed in a pictureBox that I put it next to the listbox.. Here are codes that I used so far:

 private void listBoxAllNames_SelectedIndexChanged(object sender, EventArgs e)
    { string imagelocation = "C:/Database/AlumniPicture/'" +listBoxAllNames.SelectedItem.ToString()+ "'.jpg";
        pictureBoxAlumnus.ImageLocation = imagelocation;
        pictureBoxAlumnus.SizeMode = PictureBoxSizeMode.AutoSize;}

In my AlumniPicture folder, I have all pictures. The pictures' file names are same as their fullNames. I don't know if there is a better approach. This is what I come up with and it doesn't work. Thank you for your replies in advance.

Upvotes: 0

Views: 104

Answers (2)

Steve Wellens
Steve Wellens

Reputation: 20620

Get those apostrophes ' out of there:

"C:/Database/AlumniPicture/"

".jpg"

Upvotes: 0

QYY
QYY

Reputation: 182

My suggestion is to change the first line to:

string imagelocation = Path.Combine(@"C:\Database\AlumniPicture", listBoxAllNames.SelectedItem.ToString()+ ".jpg");

Upvotes: 1

Related Questions