1,105,242 Community Members

Automated ListBoxes

Member Avatar
jjsmithy
Newbie Poster
8 posts since Mar 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hi everyone; I have a folder with images in, all JPG, and when the application starts up I want a listbox to display all of the image names, automated from the folder;

Just wondering how to do that and if anyone can help?

Thanks :)

Member Avatar
thines01
Postaholic
2,420 posts since Oct 2009
Reputation Points: 389 [?]
Q&As Helped to Solve: 413 [?]
Skill Endorsements: 10 [?]
Team Colleague
Featured
 
0
 

I would do it like this:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DW_417279_CS_FORM
{
   public partial class Form1 : Form
   {
      public Form1()
      {
         InitializeComponent();
      }

      private void Form1_Load(object sender, EventArgs e)
      {
         List<string> lst_str = Directory.GetFiles(@"C:\Users\USERNAMEGOESHERE\Pictures\", "*.jpg").ToList();
         lst_str.ForEach(s => listBox1.Items.Add(s));
      }
   }
}
Member Avatar
jjsmithy
Newbie Poster
8 posts since Mar 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Thank you for your help; however when it runs the listbox remains empty

Does this code look alright? I.e name of the namespace and link to the picture library.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
 
namespace WindowsFormsApplication4
{
   public partial class Form1 : Form
   {
      public Form1()
      {
         InitializeComponent();
      }
 
      private void Form1_Load(object sender, EventArgs e)
      {
          List<string> lst_str = Directory.GetFiles(@"C:\Users\James\Pictures\", "*.jpg").ToList();
         lst_str.ForEach(s => listBox1.Items.Add(s));
      }
   }
}
Member Avatar
thines01
Postaholic
2,420 posts since Oct 2009
Reputation Points: 389 [?]
Q&As Helped to Solve: 413 [?]
Skill Endorsements: 10 [?]
Team Colleague
Featured
 
0
 

Yes. You have pictures in that directory, right?

Member Avatar
jjsmithy
Newbie Poster
8 posts since Mar 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Yer there, and I tried the other folder where all the pictures are which is:
C:\Users\James\My Documents\Assessment\ImageFolder\Photos\

Both attempts are coming back with an empty listbox

Member Avatar
thines01
Postaholic
2,420 posts since Oct 2009
Reputation Points: 389 [?]
Q&As Helped to Solve: 413 [?]
Skill Endorsements: 10 [?]
Team Colleague
Featured
 
0
 

If you put a breakpoint on the ForEach, and run the program and inspect the contents of the lst_str, is it actually empty?

Member Avatar
jjsmithy
Newbie Poster
8 posts since Mar 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Define inspect the lst_str? I put a breakpoint in and it ran like it did before; just showing an empty listbox.

Member Avatar
thines01
Postaholic
2,420 posts since Oct 2009
Reputation Points: 389 [?]
Q&As Helped to Solve: 413 [?]
Skill Endorsements: 10 [?]
Team Colleague
Featured
 
0
 

Add a "watch" on it in debug mode or hover it with the mouse and a + sign will appear. You can click on that to see the contents.

Member Avatar
ChrisHunter
Practically a Master Poster
625 posts since Feb 2011
Reputation Points: 120 [?]
Q&As Helped to Solve: 69 [?]
Skill Endorsements: 15 [?]
Featured
 
0
 

i've only ever used a backend database to populate a list box but i don't know about the "Directory.GetFiles(@"C:\Users\James\Pictures\", "*.jpg").ToList();" part, but to put the names in the list box try this (the foreach section)

private void popListBox()
        {
            try
            {
                List<string> imgNames = Directory.GetFiles(@"C:\Users\James\Pictures\", "*.jpg").ToList();

                foreach (string str in imgNames )
                {
                    //Adds new item to listBox1 foreach item in sting list
                    listBox1.Items.Add(str);
                }
            }
            catch
            {
            }
        }

Hope this helps.

Member Avatar
thines01
Postaholic
2,420 posts since Oct 2009
Reputation Points: 389 [?]
Q&As Helped to Solve: 413 [?]
Skill Endorsements: 10 [?]
Team Colleague
Featured
 
0
 

What I'm asking is: Does the list have any data in it right BEFORE the attempt to put it in the ListBox?
If that directory actually has .jpg files in it, that code will find it. I did not test it for case-sensitivity, however.


@ChrisHunter:
That's effectively the same code.
I used List<T> because it has a built-in ForEach().

I also could have done:

Directory.GetFiles(@"C:\Users\James\Pictures\", "*.jpg").ToList().ForEach(s => listBox1.Items.Add(s));
Member Avatar
Mitja Bonca
Posting Maven
2,561 posts since May 2009
Reputation Points: 557 [?]
Q&As Helped to Solve: 489 [?]
Skill Endorsements: 21 [?]
 
0
 

or this:

string dirPatgh = @"C:\MyFolder\";
DirectoryInfo di = new DirectoryInfo(dirPath);
// Get a reference to each file in that directory.
FileInfo[] files = di.GetFiles();
foreach(FileInfo file in files)
{
   listbox1.Items.Add(file.Name);
}
Member Avatar
jjsmithy
Newbie Poster
8 posts since Mar 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

All the Pictures folder holds is the jpg images; while the code I am testing out is a new project so this is the only code.

Thanks for your suggestions but the listbox is still empty; could the problem be with the Listbox properties or the .Designer?

Member Avatar
ChrisHunter
Practically a Master Poster
625 posts since Feb 2011
Reputation Points: 120 [?]
Q&As Helped to Solve: 69 [?]
Skill Endorsements: 15 [?]
Featured
 
0
 

try this, it worked for me but i was looking for all Xml file names and used a comboBox instead of a listBox (i've used your directory in the example).

private void locatetFiles()
        {
            ListBox1.Items.Clear();

            try
            {
                foreach (string file in Directory.GetFiles(@"C:\Users\James\Pictures\", "*.jpg", SearchOption.AllDirectories))
                {
                    ListBox1.Items.Add(file);
                }
            }
            catch (Exception e)
            {
                MessageBox.Show("" + e);
            }
        }

Hope this is better for you.

Member Avatar
jjsmithy
Newbie Poster
8 posts since Mar 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hmm still not working :/ Just showing this form: (attached)

Attachments form1notworking.png 35.72KB
Member Avatar
ChrisHunter
Practically a Master Poster
625 posts since Feb 2011
Reputation Points: 120 [?]
Q&As Helped to Solve: 69 [?]
Skill Endorsements: 15 [?]
Featured
 
2
 

This works i've tried and tested it, just put your directory in, call the method in the constructor or where ever you want and you will be good to go. (it shows the whole directors as well as the name, i don't know how to just return the name sorry).

private void locatetFiles()
        {
            listBox1.Items.Clear();

            try
            {
                foreach (string file in Directory.GetFiles(@"C:\Users\user_name\Pictures", "*.jpg", SearchOption.AllDirectories))
                {   
                    listBox1.Items.Add(file);
                }
            }
            catch (Exception e)
            {
                MessageBox.Show("" + e);
            }
        }

Let me know how you get on.

Member Avatar
jjsmithy
Newbie Poster
8 posts since Mar 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Got it!
Thank you all very much for your help; this has really been bugging me for the last week, so I am glad I can move on now to the next bit of this project.

Thanks again :)

Question Answered as of 2 Years Ago by thines01, ChrisHunter and Mitja Bonca
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article