Elliott Hawkins - 1 year ago 54
Vb.net Question

# How to display a pictureBox if a certain value is displayed in a label

I am currently writing a Blackjack game for a school project. (below is my code)

``````Public Class frmBlackJack
Public Counter As Integer = 0

Private Sub btnDrawCard_Click(sender As Object, e As EventArgs) Handles btnDrawCard.Click
Randomize()
Counter = Counter + 1
Dim Card1 As Integer
Dim Card2 As Integer
Dim Card3 As Integer
Dim Card4 As Integer
Dim Card5 As Integer

If Counter >= 1 Then
btnCheckScore.Enabled = True
End If
If Counter = 1 Then
Card1 = (10 * Rnd() + 1)
lblCard1.Text = Card1
lblCard1.Visible = True
End If
If Counter = 2 Then
Card2 = (10 * Rnd() + 1)
lblCard2.Text = Card2
lblCard2.Visible = True
End If
If Counter = 3 Then
Card3 = (10 * Rnd() + 1)
lblCard3.Text = Card3
lblCard3.Visible = True
End If
If Counter = 4 Then
Card4 = (10 * Rnd() + 1)
lblCard4.Text = Card4
lblCard4.Visible = True
End If
If Counter = 5 Then
Card5 = (10 * Rnd() + 1)
lblCard5.Text = Card5
lblCard5.Visible = True
End If
If Counter > 5 Then
MsgBox("You can't draw anymore cards!")
End If
End Sub

Dim ComputerCard1 As Integer
Dim ComputerCard2 As Integer
Dim ComputerCard3 As Integer

ComputerCard1 = (10 * Rnd() + 1)
ComputerCard2 = (10 * Rnd() + 1)
ComputerCard3 = (10 * Rnd() + 1)

lblComputerCard1.Text = ComputerCard1
lblComputerCard2.Text = ComputerCard2
lblComputerCard3.Text = ComputerCard3

lblComputerScore.Visible = False
lblYourScore.Visible = False

btnCheckScore.Enabled = False

End Sub

Private Sub btnCheckScore_Click(sender As Object, e As EventArgs) Handles btnCheckScore.Click
Dim PlayerSum As Integer
Dim ComputerSum As Integer

Dim youWonTitle As String = "You Won!"
Dim youWonMsg As String = "Great work! Play again?"
Dim youLostTitle As String = "You Lost!"
Dim youLostMsg As String = "Better luck next time! Play again?"
Dim youDrewTitle As String = "You Drew"
Dim youDrewMsg As String = "That was close! Play again?"

Dim style As MsgBoxStyle = MsgBoxStyle.YesNo

lblComputerCard1.Visible = True
lblComputerCard2.Visible = True
lblComputerCard3.Visible = True

btnDrawCard.Enabled = False

If Counter < 1 Then
PlayerSum = Val(lblCard1.Text)
lblPLayerScore.Text = PlayerSum
lblPLayerScore.Visible = True
lblYourScore.Visible = True
End If
If Counter > 1 Then
PlayerSum = Val(lblCard1.Text) + Val(lblCard2.Text)
lblPLayerScore.Text = PlayerSum
lblPLayerScore.Visible = True
lblYourScore.Visible = True
End If
If Counter > 2 Then
PlayerSum = Val(lblCard1.Text) + Val(lblCard2.Text) + Val(lblCard3.Text)
lblPLayerScore.Text = PlayerSum
lblPLayerScore.Visible = True
lblYourScore.Visible = True
End If
If Counter > 3 Then
PlayerSum = Val(lblCard1.Text) + Val(lblCard2.Text) + Val(lblCard3.Text) + Val(lblCard4.Text)
lblPLayerScore.Text = PlayerSum
lblPLayerScore.Visible = True
lblYourScore.Visible = True
End If
If Counter > 4 Then
PlayerSum = Val(lblCard1.Text) + Val(lblCard2.Text) + Val(lblCard3.Text) + Val(lblCard4.Text) + Val(lblCard5.Text)
lblPLayerScore.Text = PlayerSum
lblPLayerScore.Visible = True
lblYourScore.Visible = True
End If

ComputerSum = Val(lblComputerCard1.Text) + Val(lblComputerCard2.Text) + Val(lblComputerCard3.Text)
lblComputerTotal.Text = ComputerSum
lblComputerScore.Visible = True
lblComputerTotal.Visible = True

If lblCard1.Text = 1 Then

End If

If PlayerSum < 21 And PlayerSum > ComputerSum Then
Dim responseYouWon = MsgBox(youWonMsg, style, youWonTitle)
If responseYouWon = MsgBoxResult.Yes Then
btnPlayAgain.PerformClick()
Else
btnQuit.PerformClick()
End If

ElseIf ComputerSum > 21 And PlayerSum < 21 Then
Dim responseYouWon = MsgBox(youWonMsg, style, youWonTitle)
If responseYouWon = MsgBoxResult.Yes Then
btnPlayAgain.PerformClick()
Else
btnQuit.PerformClick()
End If

ElseIf ComputerSum > 21 And ComputerSum < 21 Then
Dim responseYouWon = MsgBox(youWonMsg, style, youWonTitle)
If responseYouWon = MsgBoxResult.Yes Then
btnPlayAgain.PerformClick()
Else
btnQuit.PerformClick()
End If

ElseIf PlayerSum = 21 And ComputerSum > 21 Then
Dim responseYouWon = MsgBox(youWonMsg, style, youWonTitle)
If responseYouWon = MsgBoxResult.Yes Then
btnPlayAgain.PerformClick()
Else
btnQuit.PerformClick()
End If

ElseIf PlayerSum = 21 And ComputerSum < 21 Then
Dim responseYouWon = MsgBox(youWonMsg, style, youWonTitle)
If responseYouWon = MsgBoxResult.Yes Then
btnPlayAgain.PerformClick()
Else
btnQuit.PerformClick()
End If

ElseIf ComputerSum < 21 And ComputerSum > PlayerSum Then
Dim responseYouLost = MsgBox(youLostMsg, style, youLostTitle)
If responseYouLost = MsgBoxResult.Yes Then
btnPlayAgain.PerformClick()
Else
btnQuit.PerformClick()
End If

ElseIf PlayerSum > 21 And ComputerSum < 21 Then
Dim responseYouLost = MsgBox(youLostMsg, style, youLostTitle)
If responseYouLost = MsgBoxResult.Yes Then
btnPlayAgain.PerformClick()
Else
btnQuit.PerformClick()
End If

ElseIf ComputerSum = 21 And PlayerSum <> 21 Then
Dim responseYouLost = MsgBox(youLostMsg, style, youLostTitle)
If responseYouLost = MsgBoxResult.Yes Then
btnPlayAgain.PerformClick()
Else
btnQuit.PerformClick()
End If

ElseIf ComputerSum = PlayerSum Then
Dim responseYouDrew = MsgBox(youDrewMsg, style, youDrewTitle)
If responseYouDrew = MsgBoxResult.Yes Then
btnPlayAgain.PerformClick()
Else
btnQuit.PerformClick()
End If

ElseIf PlayerSum > 21 And ComputerSum > 21 Then
Dim responseYouDrew = MsgBox(youDrewMsg, style, youDrewTitle)
If responseYouDrew = MsgBoxResult.Yes Then
btnPlayAgain.PerformClick()
Else
btnQuit.PerformClick()
End If
End If
End Sub

Private Sub btnPlayAgain_Click(sender As Object, e As EventArgs) Handles btnPlayAgain.Click
lblComputerCard1.Visible = False
lblComputerCard2.Visible = False
lblComputerCard3.Visible = False
lblComputerCard1.Text = ""
lblComputerCard2.Text = ""
lblComputerCard3.Text = ""

lblCard1.Visible = False
lblCard2.Visible = False
lblCard3.Visible = False
lblCard4.Visible = False
lblCard5.Visible = False
lblCard1.Text = ""
lblCard2.Text = ""
lblCard3.Text = ""
lblCard4.Text = ""
lblCard5.Text = ""
Counter = 0

Dim ComputerCard1 As Integer
Dim ComputerCard2 As Integer
Dim ComputerCard3 As Integer

ComputerCard1 = (10 * Rnd() + 1)
ComputerCard2 = (10 * Rnd() + 1)
ComputerCard3 = (10 * Rnd() + 1)
lblComputerCard1.Text = ComputerCard1
lblComputerCard2.Text = ComputerCard2
lblComputerCard3.Text = ComputerCard3

btnDrawCard.Enabled = True
btnCheckScore.Enabled = True

lblComputerTotal.Text = ""
lblPLayerScore.Text = ""
lblComputerTotal.Visible = False
lblPLayerScore.Visible = False
lblComputerScore.Visible = False
lblYourScore.Visible = False

End Sub

Private Sub btnQuit_Click(sender As Object, e As EventArgs) Handles btnQuit.Click
Me.Close()
End Sub

btnDrawCard.PerformClick()

End Sub

Me.Close()
End Sub
``````

End Class

What I want to happen, is for a card (picture) to be displayed, if a certain number is also displayed in a label. For example if the value 1 is displayed in lblCard1 then picture of an ace pops up on the form.

(Disclaimer) Not sure if this is actually possible. I have been searching around on the internet for a solution however I couldn't find anything. If you know how to do this please respond.

Thanks

Firstly, I'd strongly recommend coding with an "Option Strict On" declaration at the very top of your form's code module rather than relying on implicit type casting.

Secondly, I'd recommend using a "Select Case" statement rather than a long series of If statements.

There are numerous ways of achieving your desired outcome. It depends on whether you want the picture box to be hidden or to be visible but displaying no image when the key number is not displayed.

Using the example of an ace being shown when lblCard1.Text = "1" and "picAce" is a picture box on your form, one approach would be:

To hide the ace:

``````picAce.Visible = (lblCard1.Text = "1")
``````

To show the picture box and load it from a file located in the path "C:\pics\ace.jpg" if "1" is displayed in lblCard1 or empty the picture box if another number is displayed:

``````picAce.Image = If(lblCard1.Text = "1", Image.FromFile("C:\pics\ace.jpg"), Nothing)
``````

You could also add the ace image to your project's or form's resources and load it from one of those repositories if you did not want to rely on sourcing the image from the file system.

EDIT:

The structure of a Select Case clause would look like this:

``````Select Case Counter

Case 1

Case 2

Case 3

Case 4

Case 5

Case Else

End Select
``````
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download