PictureBox initial image - vb.net

Is there a way that you can set all of the picboxes to a initial image when they are loaded? Such as: when you start a round, all the images are set to a image of a card facing down, then when the round starts it shows an image.
EDIT: How do I clear what is in the PicCard.Image?
Public Class Form1
Private Cards As New List(Of PictureBox)
Private randomnumber As Integer
Private HowManyCards As Integer
Private Sub SetupCards(numberofcards As Integer)
ClearGame()
For i As Integer = 0 To numberofcards
Dim PicCard As PictureBox = New PictureBox()
PicCard.Width = 100
PicCard.Height = 200
PicCard.Top = 50
PicCard.Left = 50 + PicCard.Width * i
Me.Controls.Add(PicCard)
PicCard.Image = imglistBackOfCard.Images(0)
Next i
End Sub
Private Sub ClearGame()
If Cards.Count > 0 Then
For i As Integer = 0 To Cards.Count - 1
Me.Controls.Remove(Cards(i))
Next
End If
' Clear the cards if they were already setup from a previous game.
Cards.Clear()
End Sub
Private Sub RandomCard()
Randomize()
randomnumber = Int(Rnd() * imglist1.Images.Count - 1) + 1
End Sub
Public Sub ShowCards(numberofcards As Integer)
For i As Integer = 0 To numberofcards
Dim PicCard As PictureBox = New PictureBox()
RandomCard()
PicCard.Width = 100
PicCard.Height = 200
PicCard.Top = 50
PicCard.Left = 50 + PicCard.Width * i
Me.Controls.Add(PicCard)
PicCard.Image = imglist1.Images(randomnumber)
PicCard.Tag = randomnumber
AddHandler PicCard.Click, AddressOf Me.cardflip_click
Cards.Add(PicCard)
Next i
End Sub
Private Sub StartGame_Click(sender As Object, e As EventArgs) Handles btnStartGame.Click
ShowCards(HowManyCards - 1)
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
HowManyCards = InputBox("How Many Cards?", "Please Enter")
SetupCards(Int(howmanycards - 1))
End Sub
End Class

Related

Updating the current datagridview instead of adding another in VB.net

Hello sorry for asking too many questions im just really a beginner on VB.net but what I wanted to do know is instead of adding another rows how can I update that instead? like Row = Bulgogi,1,45 and then when I click on the picturebox again it will be Row = Bulgogi,2,90 and not adding Bulgogi, 1 ,45 and Bulgogi, 2 ,90 also the Quantity isnt working just stuck in 1 everytime it clicks but the increment is working fine :/
Private BibimbapQuantity = 0
Private BulgogiQuantity = 0
Private BibimbapPrice As Integer
Private BulgogiPrice As Integer
Private TotalPriceInt As Integer
Private Sub PictureBox2_Click(sender As Object, e As EventArgs) Handles buttonBibimbap.Click
BibimbapQuantity += 1
BibimbapPrice = 45 * BibimbapQuantity
Me.DataGridView2.Rows.Add("Bibimbap", BibimbapQuantity, BibimbapPrice)
totalPrice.Text = BibimbapPrice + BulgogiPrice
End Sub
Private Sub buttonBulgogi_Click(sender As Object, e As EventArgs) Handles buttonBulgogi.Click
BulgogiQuantity += 1
BulgogiPrice = 50 * BulgogiQuantity
Dim Satisfy As Integer = 0
Me.DataGridView2.Rows.Add("Bulgogi", BulgogiQuantity, BibimbapPrice)
totalPrice.Text = BibimbapPrice + BulgogiPrice
End Sub
Try to add this before the Me.DataGridView2.Rows.Add
Me.DataGridView2.Rows.Clear()
See the comments in the AddItemToCart() method for explanations.
Public Class Form1
Dim bibimbapQuantity, bulgogiQuantity, kimchiQuantity As Integer
Dim bibimbapTotalPrice, bulgogiTotalPrice, kimchiTotalPrice As Integer
Const bibimbapPrice As Integer = 45, bulgogiPrice As Integer = 50, kimchiPrice As Integer = 35
Private Sub buttonBibimbap_Click(sender As Object, e As EventArgs) Handles buttonBibimbap.Click
bibimbapQuantity += 1
bibimbapTotalPrice = bibimbapPrice * bibimbapQuantity
AddItemToCart("Bibimbap", bibimbapQuantity, bibimbapTotalPrice)
End Sub
Private Sub buttonBulgogi_Click(sender As Object, e As EventArgs) Handles buttonBulgogi.Click
bulgogiQuantity += 1
bulgogiTotalPrice = bulgogiPrice * bulgogiQuantity
AddItemToCart("Bulgogi", bulgogiQuantity, bulgogiTotalPrice)
End Sub
Private Sub buttonKimchi_Click(sender As Object, e As EventArgs) Handles buttonKimchi.Click
kimchiQuantity += 1
kimchiTotalPrice = kimchiPrice * kimchiQuantity
AddItemToCart("Kimchi", kimchiQuantity, kimchiTotalPrice)
End Sub
Sub AddItemToCart(foodName As String, quantity As Integer, totalPrice As Integer)
Dim foundFood As Boolean = False ' initially the food name is not found yet
For i = 0 To DataGridView1.Rows.Count - 1 ' loop each row in grid
If DataGridView1(0, i).Value = foodName Then ' if current row is the food you're adding
foundFood = True ' food is found
DataGridView1(1, i).Value = quantity ' set new quantity
DataGridView1(2, i).Value = totalPrice ' set new total price
Exit For ' don't check the rest of the rows because it has been found
End If
Next
If Not foundFood Then ' if food is not found in the grid
DataGridView1.Rows.Add(foodName, quantity, totalPrice) ' add a new line in the grid
End If
textboxTotalPrice.Text = bibimbapTotalPrice + bulgogiTotalPrice + kimchiTotalPrice ' calculate total for all foods
End Sub
End Class

How can I use collision detection with spawned arrays

Since I have been trying to make a space invaders style game I have been having trouble with collision detection with spawned objects in arrays (and having a bit of trouble with the bullets, they keep stopping and having another generate). I am new at coding and would like some help with these issues, or at least some links to some forums that had the same thread question.
here is my code:
Public Class Form1
'global variables
Dim intAmountOfEnemys As Short = 9
Dim intRowsOfEnemys As Integer = 0 '**
Dim intAmountOfBullets As Integer = 0
Dim picEnemysWave1(intAmountOfEnemys) As PictureBox
Dim lblBullets As New Label
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Welcome_Screen.Hide()
Call EnemyWaves(picEnemysWave1)
End Sub
Sub PlayerMovement(ByVal sender As Object, ByVal e As KeyEventArgs) Handles MyBase.KeyDown
If e.KeyCode = Keys.A Then
If picShip.Right <= 0 Then
picShip.Left = 1567
Else
picShip.Left -= 10
End If
ElseIf e.KeyCode = Keys.D Then
If picShip.Left >= 1567 Then
picShip.Left = -15
Else
picShip.Left += 10
End If
ElseIf e.KeyCode = Keys.Space Then
Do
BulletGeneration(lblBullets)
Loop Until Keys.Space
lblBullets.Left = (picShip.Left + 7)
End If
End Sub
#Region "Enemy waves, Movement, and Properties"
Sub EnemyWaves(ByRef picEnemysWave1() As PictureBox)
'Enemy Generator
Const srtENEMYSPACING_Y As Short = 155
For intCounterForEnemys As Integer = 0 To intAmountOfEnemys
Dim intEnemySpacing As Integer = srtENEMYSPACING_Y * intCounterForEnemys
picEnemysWave1(intCounterForEnemys) = New PictureBox
picEnemysWave1(intCounterForEnemys).Location = New Point(42 + intEnemySpacing, 1)
picEnemysWave1(intCounterForEnemys).Image = My.Resources.enemy
picEnemysWave1(intCounterForEnemys).Width = 124
picEnemysWave1(intCounterForEnemys).Height = 84
picEnemysWave1(intCounterForEnemys).Show()
Me.Controls.Add(picEnemysWave1(intCounterForEnemys))
Next intCounterForEnemys
End Sub``
Private Sub TmrAlien1_Tick(sender As Object, e As EventArgs) Handles TmrAlien1.Tick
For intRandom As Integer = 0 To 9
picEnemysWave1(intRandom).Top += 3
Dim intRandomNum As Integer = Rnd()
If intRandomNum > 0.66 Then
picEnemysWave1(intRandom).Left += 2 'goes left randomly
ElseIf intRandomNum < 0.33 Then
picEnemysWave1(intRandom).Left -= 2 'goes right randomly
End If
If picEnemysWave1(intRandom).Top <= 0 Then
TmrAlien1.Start()
End If
If picEnemysWave1(intRandom).Top >= 952 Then
TmrAlien1.Stop()
End If
Next intRandom
End Sub
#End Region
#Region "Bullet Generation, Movement, and Properties"
Sub BulletGeneration(ByRef lblBullets As Object)
'Generation of Bullets
For intBulletCounter As Integer = 0 To intAmountOfBullets
lblBullets = New Label
lblBullets.location = New Point(760, 785)
lblBullets.image = My.Resources.blast2
lblBullets.width = 32
lblBullets.height = 64
lblBullets.show()
Me.Controls.Add(lblBullets)
Next intBulletCounter
End Sub
Private Sub tmrBullets_Tick(sender As Object, e As EventArgs) Handles tmrBullets.Tick
lblBullets.Top -= 20
End Sub
#End Region
#Region "Collision Detection"
Sub BulletCollision(ByRef lblBullets As Label, ByRef intAmontOfEnemys As Integer)
For Each picEnemy As PictureBox In picEnemysWave1
If lblBullets.Bounds.IntersectsWith(picEnemy.Bounds) Then
picEnemy.Location = New Point(3900, 8700)
Exit For
End If
Next
'what Im trying
End Sub
#End Region

My counter adds 1 but doesn't update properly

This is a slot machine program. I am trying to detect how many times the user clicks a button (spins). But I can't figure out why my counter only adding 1 to my clickLabel? I'm sure it's a simple fix but I'm drawing a blank.
Public Class MainForm
Private Sub clickHereButton_Click(sender As Object, e As EventArgs) Handles clickHereButton.Click
' simulates a slot machine
Dim randGen As New Random
Dim leftIndex As Integer
Dim centerIndex As Integer
Dim rightIndex As Integer
Dim counter As Integer = 1
clickHereButton.Enabled = False
For spins As Integer = 1 To 10
leftIndex = randGen.Next(0, 6)
leftPictureBox.Image = ImageList1.Images.Item(leftIndex)
Me.Refresh()
System.Threading.Thread.Sleep(50)
centerIndex = randGen.Next(0, 6)
centerPictureBox.Image = ImageList1.Images.Item(centerIndex)
Me.Refresh()
System.Threading.Thread.Sleep(50)
rightIndex = randGen.Next(0, 6)
rightPictureBox.Image = ImageList1.Images.Item(rightIndex)
Me.Refresh()
System.Threading.Thread.Sleep(50)
Next spins
If leftIndex = centerIndex AndAlso
leftIndex = rightIndex Then
MessageBox.Show("Congratulations!", "Winner", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
counter += 1
clickLabel.Text = counter.ToString()
clickHereButton.Enabled = True
clickHereButton.Focus()
End Sub
Private Sub exitButton_Click(sender As Object, e As EventArgs) Handles exitButton.Click
Me.Close()
End Sub
End Class
What's happening is you're always setting the counter to 1 everytime you click the button because it is inside the clickHereButton_Click. So even though you are incrementing it, at the beginning of your sub you are still setting it to 1.
Dim counter As Integer = 1
Private Sub clickHereButton_Click(sender As Object, e As EventArgs) Handles clickHereButton.Click
...
End Sub

VB Classic Poker. Detecting a winning hand using modulo

I need to create a poker game in vb as work for my class using mod13 for each suite to evaluate the winning hand... but I am at lost here. I just can't get how to use modulo.
I really need an hint how to do it. (this is what I have coded so far....)
Public Class Form1
Dim Cards(4) As PictureBox
Dim Hand(4) As Integer
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Creat_poker()
End Sub
Private Sub Creat_Poker_interface()
Dim i As Integer
For i = 0 To pic.GetUpperBound(0)
Cards(i) = New PictureBox
With Cards(i)
.Visible = True
.Width = 130
.Height = 200
.Left = 20 + (i * 160)
.Top = 20
.BorderStyle = BorderStyle.Fixed3D
.SizeMode = PictureBoxSizeMode.StretchImage
End With
Me.Controls.Add(pic(i))
AddHandler Cards(i).Click, AddressOf CardsSelection
Next
End Sub
Private Sub cmdNewGame_Click(sender As Object, e As EventArgs) Handles cmdNewGame.Click
Dim i As Integer
Dim hasard As New Random
For i = 0 To main.GetUpperBound(0)
Hand(i) = hasard.Next(52)
Next
For i = 0 To main.GetUpperBound(0)
Cards(i).Image = imaCards.Images(Hand(i))
Next
End Sub
End Class

Possible to select multiple tiles (pictureboxes)

Hey so I'm trying to create this little bot.. However I got this code from a friend, it's a small 5x5 tiles, I can only select one tile with this and it will only use the one tile. I would like to be able to select multiple tiles and then the bot will also use the multiple tiles registered.
Tiles code is
Public Class Tiles
Private _activeTile As Integer = 0
Public ReadOnly Property activeTile As Integer
Get
Return _activeTile
End Get
End Property
Private Sub PictureBox_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click, PictureBox2.Click, PictureBox3.Click, PictureBox4.Click, PictureBox5.Click, PictureBox6.Click, PictureBox7.Click, PictureBox8.Click, PictureBox9.Click, PictureBox10.Click, PictureBox11.Click, PictureBox12.Click, PictureBox13.Click, PictureBox14.Click, PictureBox15.Click, PictureBox16.Click, PictureBox17.Click, PictureBox18.Click, PictureBox19.Click, PictureBox20.Click, PictureBox21.Click, PictureBox22.Click, PictureBox23.Click, PictureBox24.Click, PictureBox25.Click
Dim p As PictureBox = TryCast(sender, PictureBox)
Dim id As Integer = CInt(p.Name.Split("x")(1))
If p.BorderStyle = Windows.Forms.BorderStyle.Fixed3D Then
p.BorderStyle = Windows.Forms.BorderStyle.None
p.BackColor = System.Drawing.Color.FromArgb(&HDD, &HDD, &HDD)
_activeTile = 0
Else
resetActiveTile()
p.BorderStyle = Windows.Forms.BorderStyle.Fixed3D
p.BackColor = System.Drawing.Color.FromArgb(&HA9, &HA9, &HA9)
_activeTile = id
End If
End Sub
Public Sub resetActiveTile()
For Each c As Control In Me.Controls
If c.Name.StartsWith("Pic") Then
Dim p As PictureBox = TryCast(c, PictureBox)
Dim id As Integer = CInt(p.Name.Split("x")(1))
p.BorderStyle = Windows.Forms.BorderStyle.None
p.BackColor = System.Drawing.Color.FromArgb(&HDD, &HDD, &HDD)
_activeTile = 0
End If
Next
End Sub
Private Sub Tiles_Load(sender As Object, e As EventArgs) Handles MyBase.Load
PictureBox1.BorderStyle = Windows.Forms.BorderStyle.Fixed3D
PictureBox1.BackColor = System.Drawing.Color.FromArgb(&HA9, &HA9, &HA9)
_activeTile = 1
End Sub
End Class
And here is the function where the active tile get used
Private Sub guess()
Try
If Tiles1.activeTile = 0 Then
BackgroundWorker1.CancelAsync()
MsgBox("Please select a tile!")
Else
s.Guess(currGame, Tiles1.activeTile)
End If
Catch ex As Exception
log(ex.ToString)
End Try
End Sub
Thanks a ton! :)