Dice Buttons Won't Allow Dice to Roll in My Game?

292 Views Asked by At

I just made some changes to my code for this app, but for some reason it is still not working. My restart button is not restarting the form, my score numbers are not showing up, the outcome results are not showing up, the dice are not rolling.

Here is the link to my GUI: https://imageshack.com/i/kpdOTIAQp

Basically, a human player is playing against a COMPUTER using VB's built in random number generator. The objective of the game is for either opponent to have a higher score than the other to win the game in a best of an odd number of matches format from 1 up to 99 ( like 1, 3, 5, 7, 9, 11, 13 and so forth).

The problem is that the dice images do not show up when you click the button.

Public Class DiceBattleForm

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

    End Sub

    Private Sub ExitButton_Click(sender As Object, e As EventArgs) Handles ExitButton.Click
        Dim RESPONSE As MsgBoxResult
        RESPONSE = MsgBox("Do you want to exit?", MsgBoxStyle.YesNo Or MsgBoxStyle.Question)
        If RESPONSE = MsgBoxResult.Yes Then
            Me.Dispose()
        End If
    End Sub

    Private Sub RollButton_Click(sender As Object, e As EventArgs) Handles RollButton.Click
        If GameTextBox.Text <= "0" Then
            MsgBox("Number of Games > 0", MsgBoxStyle.Critical)
            Application.Restart()
        End If

        If GameTextBox.Text > "0" Then
            GameTextBox = Convert.ToInt32(GameTextBox.Text)
        End If


        DisplayDie(PCDice1)
        DisplayDie(PCDice2)
        DisplayDie(PCDice3)

        DisplayDie(YouDice1)
        DisplayDie(YouDice2)
        DisplayDie(YouDice3)
    End Sub

    Sub DisplayDie(diePictureBox As PictureBox)
        'GENERATE random integer in range 1 to 6
        Dim face As Integer = randomObject.Next(1, 7)

        'retrieve specific die image from resources
        Dim pictureResource = My.Resources.ResourceManager.GetObject(String.Format("die{0}", face))

        'Convert pictureResource to type Image and display in ImageBox
        diePictureBox.Image = CType(pictureResource, Image)
    End Sub ' DisplaDie


    Private Sub GameTextBox_Validating(sender As Object, e As EventArgs) Handles GameTextBox.TextChanged
        If GameTextBox.Text > "0" Then
            Do
                MsgBox(GameTextBox.Text + " round(s) will be played enjoy!")
            Loop While GameTextBox.Text = "STOP"
        End If
        GameTextBox.Enabled = False
    End Sub

    Private Sub RestartButton_Click(sender As Object, e As EventArgs) Handles RestartButton.Click
        ' Reset or clear any controls (recommended)
        DiceBattleForm.Clear()
        ' NOT recommended:
        ' Application.Restart()
    End Sub
End Class
2

There are 2 best solutions below

3
On

well, for the restart button, instead of Form1.Clear()

Try

TextBox1.Clear()
TextBox2.Clear()
TextBox3.Clear()
TextBox4.Clear()

and so on that should clear the textboxes, and as for the. But really, I don't see what's wrong with the Application.Restart()

18
On

I would do it slightly differently. Rather than loading the dice images from resources, load them from an imagelist so that the index (represented by the Random result) is all you need to find the image. Loading from resources by screwing together and integer and name may be the problem - we cant tell because we cant see what the die images are named in your resources.

Also, this type of problem is ideal to learn how to debug: set a breakpoint in DisplayDie and see why it was not working: e.g. is the String.Format result correct for the names of the images?

Roll Dice, display image from an ImageList:

Private Sub btnRoll_Click(sender As Object, e As EventArgs) Handles btnRoll.Click
    ' pbC1 etc are MY picturebox names - use yours
    DisplayDie(pbC1)
    DisplayDie(pbC2)
    DisplayDie(pbC3)

End Sub

Private Sub DisplayDie(diePictureBox As PictureBox)

    Dim face As Integer = randomObject.Next(1, 7)

    ' adjust pip count to image index by subtracting one
    diePictureBox.Image = ImageList1.Images(face - 1)

End Sub

The images are listed in the ImageList in pip order, so the only transform is to convert from pips (1-6) to index (0-5).

enter image description here