Hi All:

I have this code below that uses an open common dialog box to dispay 5 horse pictures.
Does anyone have the code to display the pictures on the Form only by passing the horse picture names as parameters when I call the subprocedure in the mnuViewNext and mnuViewPrevious event handlers?
I set only 2 horse names because I will add 3 names in the add horses event handler later
Thanks

Public Class Form1
Inherits System.Windows.Forms.Form


Dim gstrShortList(4) As String
Dim gintCurrentRecord As Integer
Dim gintNumberOfHorses(4) As Integer


Private Sub DisplayPicture(ByVal intRecord As Integer)

If intRecord < 0 Then
intRecord = 0
End If
If gintNumberOfHorses.IndexOf(gintNumberOfHorses, 0.0) <> -1 Then
If intRecord > gintNumberOfHorses.IndexOf(gintNumberOfHorses, 0.0) - 1 Then
intRecord = gintNumberOfHorses.IndexOf(gintNumberOfHorses, 0.0) - 1
End If
End If
gintCurrentRecord = intRecord
PictureBox1.Image = Image.FromFile(gstrShortList(gintCurrentRecord))
End Sub

Private Function CountHorses() As Integer
Dim intTotalHorses As Integer
Dim intValue As Integer
For Each intValue In gintNumberOfHorses
intTotalHorses += gintNumberOfHorses(intValue)
Next

Return intTotalHorses
End Function

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
gstrShortList(0) = "C:\Horse Arabian.jpg"
gstrShortList(1) = "C:\Horse Mustang.jpg"
gintNumberOfHorses(0) = 0
gintNumberOfHorses(1) = 1
End Sub

Private Sub mnuFileHorsePictures_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuFileHorsePictures.Click
With OpenFileDialog1
With OpenFileDialog1
.Title = "Choose a Picture to Display"
.InitialDirectory = "C:\My HORSES & NICE HORSES Files"
.CheckFileExists = True
.Filter = "Picture files (*.jpg)/*.jpg"
.ShowDialog()
PictureBox1.Image = Image.FromFile(.FileName)

stbInfo.Text = Convert.ToString(CountHorses())


End With
End With
End Sub

Private Sub mnuFileExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuFileExit.Click
Close()
End Sub

Private Sub mnuViewNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuViewNext.Click
DisplayPicture(gintCurrentRecord + 1)
End Sub

Private Sub mnuViewPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuViewPrevious.Click
DisplayPicture(gintCurrentRecord - 1)
End Sub
End Class

:)

You can, however you need to model the data differently. I would create a class that has two properties:

Public Class MyHorses

    ' Horse Picture Name
    Public Property Caption As String

    ' The file name
    Public Property FileName As String

End Class

You then have a List(Of MyHorses) to hold the classes. When you add a horse from the CommonDialogBox simply add the class with the properties set to the list.

        ' The List of Horses
        Dim HorseList As List(Of MyHorses)

        ' When you add a new horse to the list

        ' Declare a new MyHorses Class
        Dim clsHorse as new MyHorses

        ' Set the properties
        MyHorses.Caption = "My awesome horse!"
        MyHorses.FileName = "C:\PathToHorse.png"

        ' Add the horse to the list.
        HorseList.Add(clsHorse)



        ' Current Index
        Dim intCurrentIndex As Integer = 0

        Private Sub mnuViewNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuViewNext.Click
            If CurrentIndex < HorseList.Count - 1 Then
                ' The index is less than the last index of Horses in the HorseList      
                ' increase the CurrentIndex by 1
                CurrentIndex += 1
                DisplayPicture(CurrentIndex) 
            End If
        End Sub 

        Private Sub mnuViewPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuViewPrevious.Click 
            If CurrentIndex > 0 Then
                ' The CurrentIndex is greater then the first index 
                ' (which is almost always 0)
                ' decrease the CurrentIndex by 1
                CurrentIndex -= 1
                DisplayPicture(gintCurrentRecord - 1) 
            End If
        End Sub 

        ' Display the image.
        Private Sub DisplayPicture(ByVal intRecord As Integer) 
            PictureBox1.Image = Image.FromFile(HorseList(CurrentRecord)) 
        End Sub 

Edited 4 Years Ago by Maligui: Code Coreections

This article has been dead for over six months. Start a new discussion instead.