Click here to Skip to main content
15,899,754 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
let me explain what is my aim.
i would like to open whole folder of files in a listbox using the folderBrowserDialog. I am able to do that. But, i also want to select the file from the listbox itself and upon selecting, i need the image to appear in the picturebox.
Now, with my code below, i'm only able to open the folder and display all the files in the listbox. It's not able to appear a single image in the picturebox.
If u're not able to understand what i'm trying to say, please comment and let me know. Thank You:)
C#
DialogResult results = folderBrowserDialog1.ShowDialog();
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
     listBox1.Items.Clear(); //Clear items in listbox1
     string folderName = this.folderBrowserDialog1.SelectedPath;   //To browse folder
     DirectoryInfo dir = new DirectoryInfo(folderName);
     FileInfo[] fileInfo = dir.GetFiles("*.*");                    //Get only files in .jpg form if GetFiles("*.jpg")
     foreach (FileInfo f in fileInfo)
     {
         listBox1.Items.Add(f);  //add files from the folder into listbox1
     }
     pictureBox1.Image = Image.FromFile(openFileDialog1.FileName); //Returns an image object located at the path specified
}

error occurs at:
pictureBox1.Image = Image.FromFile(openFileDialog1.FileName); //Returns an image object located at the path specified

A Million Thanks!!
Cheers!! :)
Posted
Updated 5-Jul-10 4:23am
v2
Comments
Abhinav S 5-Jul-10 10:24am    
What error do you get?

Yes, where did you set openFileDialog1.FileName,

First of all your code seems that you are calling folderBrowserDialog1.ShowDialog twice and didnt called ShowDialog of openFileDialog1.

You should select a file, or otherwise you should gt the FileInfo object for which you want to load the picture and select the file.
 
Share this answer
 
Comments
AceKiller 5-Jul-10 21:24pm    
if i were to call openFileDialog1.ShowDialog,isn't it true tat a openFileDialog will appear?i do not wan tat, i wan to select the file from the listbox instead of from the openFileDialog.
For that you need to use listBox1.SelectedItem.

Remember, you need to convert it to FileInfo again.
 
Share this answer
 

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