Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi,

can any one give example how to clear images displayed in a listbox.

I have listbox and two buttons on the form.
when i click on first button(to select path of the images folder). and second button to display images in the listbox. for this i have talen a ImagesList.And all the images are displaying in Listbox.

Now when i select the other images folder, and click on second button all the images from the listbox should clear and display the next selected folder images.

error: The process cannot access the file(showing first image name in the folder) because it is being used by another process.

I want when i click on show images button second time it should claer all he images from the listbox displaying and display the selected folder images in the listbox.

Showimages button Code:

listbox1.itemssource=null;
listbox1.items.Clear();
List<bitmapimages> imageslist=new List<bitmapimages>

System .IO .DirectoryInfo myimagesdir=new System.IO.DirectoryInfo("selected directory name");
foreach(System.IO.FileInfo myimagesfile in myimagesdir.GetFiles("*.jpg"))
{
 ...
 ...
}
listbox1 .ItemsSource =imageslist;
</bitmapimages></bitmapimages>


Thanks,
Posted
Updated 12-Aug-11 2:35am
v2
Comments
Suresh Suthar 12-Aug-11 5:51am    
Can you post your code snippet?
Pritesh Aryan 12-Aug-11 6:46am    
Code requires for database desigh??/!!!
KiranBabu M 12-Aug-11 8:37am    
I have updated the question and what the error is
plz suggest me idea to accomplish it.
BoxyBrown 12-Aug-11 6:09am    
Try bind data or refresh after you clear control. But better to look at code.
BoxyBrown 12-Aug-11 6:53am    
<pre lang="cs">private void Button1_Click(object sender, EventArgs e)
{
if(isFirst)
{
listBox1.Items.Add("1");
listBox1.Items.Add("2");

isFirst = false;
}
else
{
listBox1.Items.Clear();
}
}</pre>

This work.

Can you post what code doesn't work?

If I got your question.
(I still don't understand what is property ItemsSource for ListBox control and what is "bitmapimages")

Try this one.

C#
if (isFirst)
{
    listBox1.DataSource = null;
    listBox1.Items.Clear();
    var imageslist = new List<string>();

    var myImagesDir = new System.IO.DirectoryInfo(@"dir path");
    foreach (System.IO.FileInfo myimagesfile
             in myImagesDir .GetFiles("*.jpg"))
    {
        imageslist.Add(myimagesfile.Name);
    }
    listBox1.DataSource = imageslist;

    isFirst = false;
}
else
{
    listBox1.DataSource = null;

    isFirst = true;
}</string>
 
Share this answer
 
I think we need to see what is in '...'. The FileInfo object is clearly not getting cleaned up correctly and that might well be because you are storing a reference to it in the list (which you shouldn't). This is easy to do as both Image.FromStream and Image.FromFile keep the stream open and therefore the file locked for the lifetime of the Image object.

So what you need to do is open the file, read the contents into a MemoryStream, and use that to create your image:
foreach(FileInfo file in files){
 FileStream fs = file.OpenRead();
 MemoryStream ms = new MemoryStream();
 fs.CopyTo(ms);
 fs.Close(); // this frees the file
 
 Image image = Image.FromStream(ms); // note: NOT 'fs' or Image.FromFile

}
 
Share this answer
 
v2

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