borderAndreaVB free resources for Visual Basic developersborder
borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2014 Andrea Tincaniborder

AndreaVB | Forum | News | Downloads | Register | Help | Member List | Statistics | Search | PM | Profile

Print This Topic
Previous Topic (CoolBar related question ?)Next Topic (Add XML Elemens to a ComboBox) New Topic New Poll Post Reply
AndreaVB Forum : VB.Net : Display pictures on Forms in VB.NET
Poster Message
tryinghard
Level: Guest


icon Display pictures on Forms in VB.NET

Hi All:

I have this code below that uses an open common dialog box to dispay 5 horse pictures.
Can someone show me how to modify 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


13-08-2004 at 10:50 AM
| Quote Reply
fabulous
Level: VB Guru


Registered: 03-08-2002
Posts: 435
icon Re: Display pictures on Forms in VB.NET

Welcome to the forum tryinghard,

I need a bit of information first. When you run this code is it bahaving in a way that you don't like or do you want to make it better.

I also have another pointer, I see that you have a function to get the number of horses, you can get this from the length property of the array

'I will just use a function in order not to break your code
Private Function CountHorses() As Integer
  Return gstrShortList.Length
End Function


Let me know about the other requirements, particularly what is working and what is not. Happy coding.  

____________________________
My boss is a Jewish Carpenter (Jesus Christ)


Brain Bench Certified VB.NET Developer

14-08-2004 at 10:04 AM
View Profile Send Email to User Show All Posts Visit Homepage | Quote Reply
tryinghard
Level: Guest

icon Re: Display pictures on Forms in VB.NET

Hi fabulous:

                 Thanx  lot for your help. I  specifically need to modify the DisplayPicture subprocedure so it uses a String to display the pictureName when passed as the only parameter.

  e.g. Private Sub DisplayPicture(ByVal pictureName As String)
Now How do I call this  DisplayPicture subprocedure in the
event procedure below to display the horse picture on the form by passing one of the horse pictureNames as the parameter in the DisplayPicture subprocedure?
When  I call DisplayPicture like this:
PictureBox1.Image = Image.FromFile(DisplayPicture("C:\Horse1.jpg"))
I get an ERROR MESSAGE :"Cannot return a subprocedure"


I  am confused because my previous DisplayPicture uses intRecord As Integer as the argument in the subprocedure and not a String.

  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

Finally I am trying to display in the first panel of status bar
the number of horses each time a user clicks the Next or Previous subMenuItem so that it will count the number of horse pictures from 1 To 10 .
Yet this code:   stbInfo.panel(0).Text = Convert.ToString(CountHorses())
also gives an Error.

Also this code: DisplayPicture(gintCurrentRecord + 1)
does not work in the mnuViewNext_Click Event Handler


Also this code DisplayPicture(gintCurrentRecord - 1)
does not work in the  mnuViewPrevious_Click Event Handler

Thanks a lot for you time. I will not forget your forum when I have to support or make donations to sites.

14-08-2004 at 04:52 PM
| Quote Reply
fabulous
Level: VB Guru


Registered: 03-08-2002
Posts: 435
icon Re: Display pictures on Forms in VB.NET

Hi,

1st of all, a Sub cannot have a return value as is implied by this code

PictureBox1.Image = Image.FromFile(DisplayPicture("C:\Horse1.jpg"))


If you want to use it like that you will have to overload it and create a function like this

Private Function DisplayPicture(ByVal _path As String) As Image
  If File.Exists(_path) Then Return Image.FromFil(_path)
End Function


You would then call it like the method you used, the code snippet I placed above. If you would like to display the image from inside the method then you can make it a Sub and do it this way:

Private Sub DisplayPicture(ByVal _path As String)
  If File.Exists(_path) Then PictureBox1.Image = Image.FromFile(_path)
End Sub


What is the error you are getting when running this code

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


and can you step into it to identify where it is occuring.

I have not run this code yet so cannot make very specific solutions. I would like to know what this code inside DisplayPicture is meant to do:

If gintNumberOfHorses.IndexOf(gintNumberOfHorses, 0.0) <> -1 Then


I will run this when I get to my computer and check out what is wrong but I think information on the specific error you are getting will greatly aid me in helping you.


____________________________
My boss is a Jewish Carpenter (Jesus Christ)


Brain Bench Certified VB.NET Developer

15-08-2004 at 02:52 PM
View Profile Send Email to User Show All Posts Visit Homepage | Quote Reply
AndreaVB Forum : VB.Net : Display pictures on Forms in VB.NET
Previous Topic (CoolBar related question ?)Next Topic (Add XML Elemens to a ComboBox) New Topic New Poll Post Reply
Surf To:


Not Logged In? Username: Password: Lost your password?
borderAndreaVB free resources for Visual Basic developersborder
borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2014 Andrea Tincaniborder