hello, actually i need some help please, lets say i have a folder on my computer that contain only jpeg pictures and i wanna load them on a win form, and then have a next and previous button on the win form that will move among the images in the folder.Sorry for my English am french.am developing in vs 2005 using vb.net
Teme64 215 Veteran Poster
Here's something to get you started
Private m_FileNames() As String
Private m_CurIndex As Integer
''' <summary>
''' Return file names
''' </summary>
''' <param name="RootPath">The path to start searching files</param>
''' <param name="FileMask">An array of file masks</param>
''' <param name="FileNames">Argument returns fully qualified file names</param>
''' <param name="ShortFileNames">Argument returns file names only</param>
''' <param name="RecurseDirs">If True, searches subdirectories too</param>
''' <remarks></remarks>
Public Sub ToolDir(ByVal RootPath As String, ByVal FileMask() As String, _
ByRef FileNames() As String, ByRef ShortFileNames() As String, _
ByVal RecurseDirs As Boolean)
'
Dim DirInfo As DirectoryInfo
Dim Files() As FileInfo
Dim OneFile As FileInfo
Dim i As Integer
Dim j As Integer
Try
If FileMask.GetUpperBound(0) < 0 Then
ReDim FileMask(0)
FileMask(0) = "*.*"
End If
i = 0
For j = 0 To FileMask.GetUpperBound(0)
ReDim Files(0)
If RecurseDirs Then
DirInfo = New DirectoryInfo(RootPath)
Files = DirInfo.GetFiles(FileMask(j), IO.SearchOption.AllDirectories)
Else
DirInfo = New DirectoryInfo(RootPath)
Files = DirInfo.GetFiles(FileMask(j), IO.SearchOption.TopDirectoryOnly)
End If
For Each OneFile In Files
ReDim Preserve FileNames(i)
ReDim Preserve ShortFileNames(i)
FileNames(i) = OneFile.FullName
ShortFileNames(i) = OneFile.Name
i += 1
Next
Next j
Catch ex As Exception
' Handle errors
End Try
End Sub
Private Sub LoadImage(ByVal ImageIndex As Integer)
' Load an image from the file
If My.Computer.FileSystem.FileExists(m_FileNames(ImageIndex)) Then
PictureBox1.Image = Image.FromFile(m_FileNames(ImageIndex))
End If
' Handle errors!
End Sub
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
'
Dim FileMask(1) As String
Dim ShortFileNames() As String
Dim RootPath As String
' Initialize variables
ReDim m_FileNames(0)
ReDim ShortFileNames(0)
m_CurIndex = 0
FileMask(0) = "*.jpg"
FileMask(1) = "*.jpeg"
RootPath = "C:\" ' Scan the whole C drive, you may change this to some user selected folder
' Disable Next/Prev buttons
Button2.Enabled = False
Button3.Enabled = False
' Search images and recurse subdirectories too
ToolDir(RootPath, FileMask, m_FileNames, ShortFileNames, True)
If Not String.IsNullOrEmpty(m_FileNames(0)) Then
' At least one image found, load it
LoadImage(m_CurIndex)
If m_FileNames.GetUpperBound(0) > 0 Then
' More than one images, enable Next button
Button2.Enabled = True
End If
End If
End Sub
Private Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
' My Next button
m_CurIndex += 1
If m_CurIndex > m_FileNames.GetUpperBound(0) Then
m_CurIndex = m_FileNames.GetUpperBound(0)
End If
If m_CurIndex = m_FileNames.GetUpperBound(0) Then
' Last image, disable Next button
Button2.Enabled = False
End If
' Enable Prev button
If m_FileNames.GetUpperBound(0) > 0 Then
' More than one images, enable Prev button
Button3.Enabled = True
End If
' Load image
LoadImage(m_CurIndex)
End Sub
Private Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button3.Click
' My Previous button
m_CurIndex -= 1
If m_CurIndex < 0 Then
m_CurIndex = 0
End If
If m_CurIndex = 0 Then
' First image, disable Prev button
Button3.Enabled = False
End If
' Enable Next button
If m_FileNames.GetUpperBound(0) > 0 Then
' More than one images, enable Next button
Button2.Enabled = True
End If
' Load image
LoadImage(m_CurIndex)
End Sub
Important parts are Public Sub ToolDir(ByVal RootPath As String, ByVal FileMask() As String, ByRef FileNames() As String, ByRef ShortFileNames() As String, ByVal RecurseDirs As Boolean)
which searches for the files and Private Sub LoadImage(ByVal ImageIndex As Integer)
which loads an image from the file to picture box.
I posted the same code in DaniWeb's Scanning Computer Music Files thread. You may take a look at that thread too. There's a bit more explanation about ToolDir procedure.
Moving between next/previous file is quite easy (Button2 and Button3). I didn't test the code, it's just to show how it could be done. Also, the "initialization" is done in the Button1 click event. You may want to do initialization in some other way.
HTH
znetweb 0 Newbie Poster
Thank Teme64 for helping me, but am getting an error, lets say for the code that start with Private Sub Button1_Click(.... i will place a command button on the form this will be ok , but what control should i put on the form for the code Public Sub ToolDir(ByVal RootPath As String, ByVal FileMask() As String, _
ByRef FileNames() As String, ByRef ShortFileNames() As String, _
ByVal RecurseDirs As Boolean)
Teme64 215 Veteran Poster
but what control should i put on the form for the code Public Sub ToolDir(ByVal RootPath As String, ByVal FileMask() As String, _
ByRef FileNames() As String, ByRef ShortFileNames() As String, _
ByVal RecurseDirs As Boolean)
None. It's a subroutine that searches for the files. And in your case it gets all the JPEG images. You can put the sub in a separate module, it doesn't have to be in the same file with form's code. It's a generic file searcher and you can use it somewhere else too, for example to search for all *.doc and *.xls files from the C-drive.
If you get some error, remember always post the exact error message and show the code where the error occurs. In that way you do get help quicker and most of all, the correct answers.
znetweb 0 Newbie Poster
hello again Teme64 well you know what i have done i have change a little the code well i mess a littles , i change this
Dim DirInfo As DirectoryInfo
Dim Files() As FileInfo
Dim OneFile As FileInfo
to this one
Dim DirInfo As IO.DirectoryInfo
Dim Files() As IO.FileInfo
Dim OneFile As IO.FileInfo
Teme64 215 Veteran Poster
My first post was missing
Imports System.IO
Sorry, my mistake :)
In the Scanning Computer Music Files thread, where's the same code, I had remembered to show correct namespace importing.
znetweb 0 Newbie Poster
its ok thank for helping me Teme64.Have a nice day
Teme64 215 Veteran Poster
Hi! Nice to hear that you got answer to your problem. Could you please mark the thread as solved. Thank you!
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.