Memory exception error

old_school

Member
Joined
Jun 9, 2011
Messages
20
Programming Experience
3-5
Two peices of code i suspect causeing the issue but not sure how to fix it:

'Section one causing issue
Private
Sub btnAddDirectory1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddDirectory1.Click
Dim openDir AsNew FolderBrowserDialog
openDir.Description =
"Select the directory with the files you wish to add..."
If openDir.ShowDialog(Me) = Windows.Forms.DialogResult.OK Then
ForEach filepath AsStringInMy.Computer.FileSystem.GetFiles(openDir.SelectedPath)
IfMy.Computer.FileSystem.GetFileInfo(filepath).Extension.ToLower = ".jpg"Or _
My.Computer.FileSystem.GetFileInfo(filepath).Extension.ToLower = ".jpeg"Or _
My.Computer.FileSystem.GetFileInfo(filepath).Extension.ToLower = ".png"Or _
My.Computer.FileSystem.GetFileInfo(filepath).Extension.ToLower = ".bmp"Or _
My.Computer.FileSystem.GetFileInfo(filepath).Extension.ToLower = ".gif"Then
filename = My.Computer.FileSystem.GetName(filepath)
IfNot imageCollection.ContainsKey(filename) Then
imageCollection.Add(filename, filepath)
cmbPaths.Items.Add(filename)
EndIf
EndIf
Next
lblCount.Text = "Count: " & (cmbPaths.Items.Count.ToString)
EndIf
EndSub


'section two causeing issue
Private
Sub cmbPaths_SelectedIndexChanged(ByVal sender AsObject, ByVal e As System.EventArgs) Handles cmbPaths.SelectedIndexChanged
Dim index AsInteger
index = imageCollection.IndexOfKey(cmbPaths.SelectedItem)
IfNot index = -1 Then
pic.Image = Image.FromFile(imageCollection.Item(cmbPaths.SelectedItem))
EndIf
EndSub
 
you really should try posting your code formatted correctly, http://www.vbdotnetforums.com/forum-feedback/14540-how-do-i-add-my-code-here.html

I'm not sure what imageCollection is suppose to be so i improvised with a dictionary. Also i'm assuming that cmbPaths is a combobox. I'm assuming you wanted to make sure that something was selected so i just used the SelectedIndex property of the combobox instead, and the rest of the code appears to work fine...
    Private imageCollection As New Dictionary(Of String, String)

    'didn't modify btnAddDirectory1_Click

    Private Sub cmbPaths_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmbPaths.SelectedIndexChanged
        If cmbPaths.SelectedIndex <> -1 Then
            pic.Image = Image.FromFile(imageCollection.Item(cmbPaths.SelectedItem.ToString))
        End If
    End Sub
 
Also, Dispose the current image before assigning a new one.
 
Back
Top