Sign in to follow this  

Trouble passing PictureBox object in VB.NET

This topic is 3663 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Greetings. I'm currently developing a Slot Machine simulation for my VB.NET class. I'm implementing a class that upon construction, is passed 4 images (for the slot wheels) and 3 picture boxes (to display the images as the slot wheels). I think that I have a problem with passing either my images, or my picture boxes. I don't think it's my images, because I added an image as a private member of that particular class to see if it would display in my textbox that way, and it didn't. This leads me to believe that I've made in error in either passing, or initializing my PictureBox objects in the SlotMachine class. Here is the code for my SlotMachine Class:
Public Class SlotMachine
    Private jackpotAmountValue As Integer
    Private creditsValue As Integer
    Private wheelPics() As Bitmap = New Bitmap(3) {}
    Private wheelbox1 As New PictureBox 'Each of these represent the boxes to display the "spinning" wheels
    Private wheelbox2 As New PictureBox
    Private wheelbox3 As New PictureBox

    Public Sub New(ByVal pic1 As Bitmap, ByVal pic2 As Bitmap, ByVal pic3 As Bitmap, ByVal pic4 As Bitmap, ByVal firstWheel As PictureBox, _
                ByVal secondWheel As PictureBox, ByVal thirdWheel As PictureBox)

        wheelbox1 = firstWheel
        wheelbox2 = secondWheel
        wheelbox3 = thirdWheel
        wheelPics(0) = New Bitmap(pic1)
        wheelPics(1) = New Bitmap(pic2)
        wheelPics(2) = New Bitmap(pic3)
        wheelPics(3) = New Bitmap(pic4)
        jackpotAmount = 200
        credits = 200

    End Sub

    Public Property jackpotAmount() As Integer
        Get
            Return jackpotAmountValue
        End Get
        Set(ByVal value As Integer)
            jackpotAmountValue = value
        End Set
    End Property

    Public Property credits() As Integer
        Get
            Return creditsValue
        End Get
        Set(ByVal value As Integer)
            creditsValue = value
        End Set
    End Property

    Private Sub DetermineOutcome(ByVal value1 As Integer, ByVal value2 As Integer, ByVal value3 As Integer)
     'Yet to be implimented
    End Sub

    Public Sub Start()  'This subroutine spins each wheel individually and uses the final positions of the arrays to calculate the outcome
        DetermineOutcome(SpinAndReturnResult(wheelbox1), SpinAndReturnResult(wheelbox2), SpinAndReturnResult(wheelbox3))
    End Sub

    Private Function SpinAndReturnResult(ByVal wheel As PictureBox)
        wheel = New PictureBox          'The code in this block is purly temporary and expirimental to see if I'm passing the images
        wheel.Image = wheelPics(0)      'and picture boxes correctly
        Return Nothing
    End Function

    Private Sub WonJackpot()
        'Not yet implimented
    End Sub

    Private Sub WonFirst()
        'Not yet implimented
    End Sub

    Private Sub WonSecond()
        'Not yet implimented
    End Sub

    Private Sub WonThird()
        'Not yet implimented
    End Sub

    Private Sub Lost()
        'Not yet implimented
    End Sub

End Class
The code in the SpinAndReturnResult() block is only test code, I've yet to impliment the logic to cycle through the images, stop, and return the position of the array to the called function DetermineOutcome(), which will compare the 3 values passed to determine the correct course of action. This is the code for my form, which instantiates the SlotMachine class:
Public Class SlotMachineGame
    Dim pic1 As New Bitmap("locations on my computer")
    Dim pic2 As New Bitmap("locations on my computer")
    Dim pic3 As New Bitmap("locations on my computer")
    Dim pic4 As New Bitmap("locations on my computer")
    Dim cosmicSlots As New SlotMachine(pic1, pic2, pic3, pic4, pcbxFirstSpinner, pcbxSecondSpinner, pcbxThirdSpinner)

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        cosmicSlots.Start()
    End Sub
End Class
Again, this is also incomplete. I want to get my output to work before I continue working on my game logic. Anyone see anything wrong? Thanks a lot for any and all help! Edit: Sorry, forgot to mention that pcbxFirstSpinner, pcbxSecondSpinner, and pcbxThirdSpinner are my PictureBox objects on the form.

Share this post


Link to post
Share on other sites
I just did, and it confirmed what I had thought. My bitmap array in the SlotMachine class is getting the images fine, but the PictureBox objects in the constructor have nothing in them as the constructor is called either. Funny thing is though, that they don't have anything before the constructor is called. Don't the components of a windows form automatically get created in some auto-generated code by Visual Studio?

Share this post


Link to post
Share on other sites
That's funny. The image shows up on the form, but according to the debugger it's empty.

Thanks for your help. I'll have to tackle this some more in the morning. It might be something silly that my fatigue is keeping me from noticing.

Share this post


Link to post
Share on other sites
Alright, I'm getting closer to solving my problem. One of my flaws is that I keep treating the SlotMachineGame class (the one that instantiates SlotMachine) like it's not a class. I found out that the reason why the PictureBoxes don't exist upon SlotMachine's creation is because the Form's components are initialized in the SlotMachineGame's constructor, but before this it's data members are initialized (which includes the creation of the SlotMachine class, which requires the yet-to-be created PictureBoxes). I suppose that I could solve this by merging the two classes together. I really wanted to create a separate class for the slot machine so that I could create various visual themes, without changing any code to the SlotMachine class. All I would have to do is pass the various images to the constructor. I'm sure I'll come up with anothing implimentation that'll work in the coming days.

Share this post


Link to post
Share on other sites
Silly me, I forgot that I could declare the SlotMachine reference with the data memebers, then initialize it in the constructor of SlotMachineGame after the creation of the components. It works now, thanks!

Wow, I was creating way too many 'New' objects while trying to get my code to work.

Share this post


Link to post
Share on other sites

This topic is 3663 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this