Iterate through array help  
Author Message
b0bd0gz





PostPosted: Visual Basic Express Edition, Iterate through array help Top

Hello,
I have a form with a picturebox and a menustrip from which the user opens a file or files using the openfiledialog and adds them to an array. Also on the menustrip there is a next button so that if the user selected more than one picture they can display the next picture they in the array.

The problem I'm having is when I use the next button it will go to next picture in the array but then when I click the next button again it doesn't do anything.

Here's the code
Public Class Form1

Private Sub Form1_Load(ByVal sender As _
System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

opnFile.Filter = "All Files (*.*)|*.*| Jpg (*.jpg)|*.jpg| Jpeg (*.jpeg)|*.jpeg"

End Sub

Private Sub OpenToolStripMenuItem_Click(ByVal sender As _
System.Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click

If opnFile.ShowDialog = Windows.Forms.DialogResult.OK Then
picBox.ImageLocation = opnFile.FileName
End If

End Sub

Private Sub NextToolStripMenuItem_Click(ByVal sender As _
System.Object, ByVal e As System.EventArgs) Handles NextToolStripMenuItem.Click

Dim picture As String
Dim pictures() As String
pictures = opnFile.FileNames

For Each picture In pictures
picBox.ImageLocation = picture
Next


End Sub
End Class

I'm still new to vb.net so any help on how to get this to work would be great.

Thanks in advance
b0bd0gz


Visual Studio Express Editions42  
 
 
nobugz





PostPosted: Visual Basic Express Edition, Iterate through array help Top

You need to keep track of which file you're displaying. Add a form member named mCurrentFile. Set it to zero in OpenToolStripMenuItem_Click. Then in NextToolStripMenuItem_Click, do something like this:
if mCurrentFile >= opnFile.FileNames.Count Then Exit Sub
mCurrentFile += 1
picture = openFile.FileNames(mCurrentFile)
... etc



 
 
b0bd0gz





PostPosted: Visual Basic Express Edition, Iterate through array help Top

Thanks for the quick reply. Just one thing stopping me from trying out your suggestion and that is I don't know how to add a form member and I couldn't find anything on google. If you could tell me how that would be great.

b0bd0gz

 
 
nobugz





PostPosted: Visual Basic Express Edition, Iterate through array help Top

Public Class Form1
Private mCurrentFile As Integer
End Class



 
 
b0bd0gz





PostPosted: Visual Basic Express Edition, Iterate through array help Top

Right I've now added a new form member and set it to 0 in OpenToolStripMenuItem_Click event. The last thing is that it says that count is not a member of system.array when I hover my cursor over opnFile.FileNames.count. Any ideas

Again thanks for the help and these fast replies

b0bd0gz

 
 
nobugz





PostPosted: Visual Basic Express Edition, Iterate through array help Top

Oops. Make that opnFile.FileNames.Length...


 
 
b0bd0gz





PostPosted: Visual Basic Express Edition, Iterate through array help Top

Right that got rid of the "Count is not a member of system.array". Now when I click next it comes up with the exception "Index was outside the bounds of the array." for the line picture = opnFile.FileNames(mCurrentFile). Is it that I have put your code in the wrong place

Private Sub NextToolStripMenuItem_Click(ByVal sender As _
System.Object, ByVal e As System.EventArgs) Handles NextToolStripMenuItem.Click


Dim picture As String
Dim pictures() As String
pictures = opnFile.FileNames

For Each picture In pictures
If mCurrentFile >= opnFile.FileNames.Length Then Exit Sub
mCurrentFile += 1
picture = opnFile.FileNames(mCurrentFile)
Next



End Sub

Thanks for the help
nobugz.

b0bd0gz


 
 
nobugz





PostPosted: Visual Basic Express Edition, Iterate through array help Top

Oops again, swap the statements:
mCurrentFile += 1
if mCurrentFile >= openFile.FileNames.Length Then Exit Sub



 
 
b0bd0gz





PostPosted: Visual Basic Express Edition, Iterate through array help Top

Hello again,
Still haven't got it to work heres what it looks like now

Dim picture As String
Dim pictures() As String
pictures = opnFile.FileNames

For Each picture In pictures
mCurrentFile += 1
If mCurrentFile >= opnFile.FileNames.Length Then Exit Sub
picture = opnFile.FileNames(mCurrentFile)
Next

Thanks again for the help

b0bd0gz

 
 
nobugz





PostPosted: Visual Basic Express Edition, Iterate through array help Top

You don't need the For Each loop anymore:

Private Sub NextToolStripMenuItem_Click(ByVal sender As _
System.Object, ByVal e As System.EventArgs) Handles NextToolStripMenuItem.Click

If mCurrentFile >= opnFile.FileNames.Length Then Exit Sub
mCurrentFile += 1
picBox.ImageLocation = opnFile.FileNames(mCurrentFile)
End Sub



 
 
b0bd0gz





PostPosted: Visual Basic Express Edition, Iterate through array help Top

It work perfectly now. Massive thanks nobugz for the help

b0bd0gz