I'm relatively new to vb .net coming from an Access VBA background. I'm wanting to become more familiar with creating and using classes.
I'm using a function to return an array of jpg image filenames that are found in a folder. I want to click a next and previous button to move through the array and dynamically show the jpg image in a picturebox. I am able to do this using global variables stored in a global module, what I would like to do is have this all working in an encapsulated class.
This is the code I'm using to call the function after a folder has been selected.....
Private Sub btnOpenFolder_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpenFolder.Click
Try
'Create a new Open Dialog object to get our image folder
Dim openDLG As New FolderBrowserDialog
openDLG.Description = "Select the directory that you want to use As the default."
openDLG.RootFolder = Environment.SpecialFolder.MyComputer
openDLG.SelectedPath = "C:\Documents and Settings\Photos\images"
If openDLG.ShowDialog = DialogResult.OK Then
'then show the folder path selected and populate the array
txtFolderPath.Text = openDLG.SelectedPath
Dim extensions() As String = {"*.jpg"}
Dim files() As FileInfo = GetFilesByExtensions(CStr(txtFolderPath.Text), extensions)
End If
Catch exc As Exception
MessageBox.Show(exc.Message, " Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
This is the global function that populates the array....
Public Function GetFilesByExtensions(ByVal strPath As String, ByVal colExtensions() As String) As FileInfo() ' The collection we will be using to store the file information in Try Dim tmpCollection As New Collection ' Loop through all the extensions and files, and add them to the collection. For Each strExtension As String In colExtensions For Each aFile As FileInfo In New DirectoryInfo(strPath).GetFiles(strExtension) tmpCollection.Add(aFile) Next Next ' Variables to convert the collection to type FileInfo (more convenient to the programmer) Dim tmpFiles(tmpCollection.Count - 1) As FileInfo, i As Integer = 0 ' Loop through the collection and convert it into FileInfo For Each aFile As FileInfo In tmpCollection tmpFiles(i) = aFile i += 1 Next ' Return the files Return tmpFiles Catch exc As Exception MessageBox.Show(exc.Message, " Error", MessageBoxButtons.OK, MessageBoxIcon.Error) End Try End Function
Thanks in advance for any help with this!
This post has been edited by DataFlowJoe: 06 December 2008 - 12:45 PM

New Topic/Question
Reply




MultiQuote


|