Results 1 to 4 of 4

Thread: Looping through each file in directory

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2006
    Posts
    3

    Smile Looping through each file in directory

    Hi,

    I have a number of bmp files in a directory, I want to loop through on the click of a button so they in turn display in the picture.image property. I have the code below which displays the first bmp correctly, how do I loop through the rest on each click of the button?

    Me.Picture1.Image = System.Drawing.Image.FromFile("C:\files\" & pageCount & ".bmp")


    Many thanks for the help

    Steven

  2. #2
    Junior Member
    Join Date
    Aug 2005
    Posts
    28

    Re: Looping through each file in directory

    How about...

    VB Code:
    1. 'Assuming that your filenames are named numerically starting with 0.
    2. Dim pageCount As Integer = 0
    3.  
    4. Private Sub Form1_Load(...)
    5.    NextPicture()
    6. End Sub
    7.  
    8. Private Sub Button1_Click(...)
    9.    NextPicture()
    10. End Sub
    11.  
    12. Private Sub NextPicture()
    13.    Me.Picture1.Image = System.Drawing.Image.FromFile("C:\files\" & pageCount & ".bmp")
    14.    pageCount = pageCount + 1
    15. End Sub
    When the form loads, it calls the NextPicture() function which displays the first picture. Each time you click the button after that it will display the next picture. Of course, you'll want to add some error checking in there too so you don't try to open a file that doesn't exist.

  3. #3
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Looping through each file in directory

    At form load event, use DirectoryInfo and its function GetFiles(".bmp") to add all .bmp file names to an arrayList, then set your pointer to zero. Set your PicBox.Image to arrayList(pointer) which now is the first element in the arraylist.
    In button click event, increase the pointer then set the picBox image again. Just make sure that the pointer doesn't out of bound. If it is, set it back to zero.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Looping through each file in directory

    Everyone seems determined to use DirectoryInfo and FileInfo objects all the time. There's no point if all you're using is the file and folder paths. Use the Directory and File classes instead, which have all Shared members and thus require no instances to be created. All you get are the String objects containing the paths, which is all you need:
    VB Code:
    1. Dim filePaths As String() = IO.Directory.GetFiles("folder path here", "*.bmp")
    2.  
    3. For Each filePath As String In filePaths
    4.     MessageBox.Show(filePath)
    5. Next filePath
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width