How to clear or initialize imagelist for began game again? - vb.net

I want to make a game for 43 image from imagelist as random and 1 picturebox for display , and I have 43 button , I would to clear imagelist when I click the button "starr" for began the game without reset form.
My code just for first 4 button
Public Class Form1
'Random Class included in .NET
Private RND As New Random
Dim ImageIndexes As New List(Of Integer)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
okkk: 'Populate the array with the Index of each Image in the ImageList
For i As Integer = 0 To ImageList1.Images.Count - 1
ImageIndexes.Add(i)
Next i
End Sub
Private Sub Buttons_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click,
Button2.Click, Button3.Click, Button4.Click
CType(sender, Button).Visible = False
Dim ArrayIndex As Integer = RND.Next(0, ImageIndexes.Count)
PictureBox1.Image = ImageList1.Images(ImageIndexes(ArrayIndex))
ImageIndexes.Remove(ArrayIndex)
End Sub
Private Sub starr_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles starr.Click
ImageIndexes.Clear()
PictureBox1.Image = Nothing
Button1.Visible = True
Button2.Visible = True
Button3.Visible = True
Button3.Visible = True
Button4.Visible = True
End Sub
End Class

Related

How to make the Form 2 and three animate while the buttons are on form 1

I want my form 2 and 3 to animate while the buttons are on form 1 and when it goes to form 2 it doesn't stop it continues animating.
FORM 1 CODE:
Public Class Form1
Private runSequence() As Image
Private walkSequence() As Image
Private idleSequence() As Image
Private runIndex As Integer = -1
Private walkIndex As Integer = -1
Private idleIndex As Integer = -1
Private WithEvents trmRun As New System.Windows.Forms.Timer
Private WithEvents trmWalk As New System.Windows.Forms.Timer
Private WithEvents trmIdle As New System.Windows.Forms.Timer
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
Form2.PictureBox1.Visible = False
Form3.PictureBox1.Visible = False
Form2.Show()
Form3.Show()
Me.Width = 300
Form2.Width = 300
Form3.Width = 300
Me.Height = 300
Form2.Height = 300
Form3.Height = 300
Me.Location = New Point(100, 100)
Form2.Location = New Point(385, 100)
Form3.Location = New Point(670, 100)
runSequence = {My.Resources.sonic1, My.Resources.sonic2,
My.Resources.sonic3, My.Resources.sonic4}
walkSequence = {My.Resources.SONICWALK1_removebg_preview, My.Resources.SONICWALK2_removebg_preview,
My.Resources.SONICWALK3_removebg_preview, My.Resources.SONICWALK4_removebg_preview,
My.Resources.SONICWALK5_removebg_preview, My.Resources.SONICWALK6_removebg_preview,
My.Resources.SONICWALK7_removebg_preview, My.Resources.SONICWALK8_removebg_preview}
idleSequence = {My.Resources.SONICIDLE1_removebg_preview, My.Resources.SONICIDLE2_removebg_preview,
My.Resources.SONICIDLE3_removebg_preview, My.Resources.SONICIDLE4_removebg_preview,
My.Resources.SONICIDLE5_removebg_preview, My.Resources.SONICIDLE6_removebg_preview,
My.Resources.SONICIDLE7_removebg_preview, My.Resources.SONICIDLE8_removebg_preview,
My.Resources.SONICIDLE9_removebg_preview}
trmRun.Interval = 100
trmRun.Enabled = False
trmWalk.Interval = 100
trmWalk.Enabled = False
trmIdle.Interval = 170
trmIdle.Enabled = False
End Sub
Private Sub btnRUN_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRUN.Click
trmWalk.Stop()
trmRun.Start()
trmIdle.Stop()
End Sub
Private Sub btnWALK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWALK.Click
trmRun.Stop()
trmWalk.Start()
trmIdle.Stop()
End Sub
Private Sub trmRun_Tick(ByVal sender As Object, ByVal e As EventArgs) Handles trmRun.Tick
runIndex = runIndex + 1
PictureBox1.Left = PictureBox1.Left + 6
If runIndex = runSequence.Length Then
runIndex = 0
Timer1.Stop()
Timer2.Start()
End If
PictureBox1.Image = runSequence(runIndex)
End Sub
Private Sub trmWalk_Tick(ByVal sender As Object, ByVal e As EventArgs) Handles trmWalk.Tick
walkIndex = walkIndex + 1
PictureBox1.Left = PictureBox1.Left + 4
If walkIndex = walkSequence.Length Then
walkIndex = 0
Timer1.Stop()
Timer2.Start()
End If
PictureBox1.Image = walkSequence(walkIndex)
End Sub
Private Sub btnIDLE_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnIDLE.Click
trmRun.Stop()
trmWalk.Stop()
trmIdle.Start()
Timer2.Stop()
End Sub
Private Sub trmIdle_Tick(ByVal sender As Object, ByVal e As EventArgs) Handles trmIdle.Tick
idleIndex = idleIndex + 1
If idleIndex = idleSequence.Length Then
idleIndex = 0
End If
PictureBox1.Image = idleSequence(idleIndex)
End Sub
Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
If PictureBox1.Left >= 275 Then
Timer2.Stop()
Form2.PictureBox1.Visible = True
Form2.Timer1.Start()
End If
End Sub
End Class
The thing is I want it to animate on form 2 and 3 without having to put buttons on form 2 and three I want the sprite to be controlled from buttons on the form 1. And I want it to be like if I pressed run on form 1 before it goes to form 2 the continuing animation will be run or walk if I pressed walk before.
Video Example of the current project:
https://youtu.be/DLb68CXy0dI

If my counter ends at 10 at textbox1(TB1) the random number won't show in my textbox2(TB2)

If my counter ends at 10 at textbox1 (TB1) the random number won't show in my textbox2(TB2)
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If TB1.Text = 10 Then
Dim num1 As Integer
Dim randomnumber As New Random
num1 = randomnumber.Next(100, 201)
TB2.Text = num1
End If
Timer1.Enabled = True
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
TB1.Text = TB1.Text + 1
If TB1.Text = 10 Then
Timer1.Enabled = False
End If
End Sub
End Class
Use a numeric variable to keep the count, not the TextBox. The TextBox.Text property is of type String, not Integer!
Putting Option Strict On at the top of your code will enforce using proper types. VB by default allows you to loosely convert between integer and string, which is poor practice.
Option Strict On
Public Class Form1
Private count As Integer
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
count = 0
Timer1.Enabled = True
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If count = 10 Then
Dim randomnumber As New Random()
Dim num1 = randomnumber.Next(100, 201)
TB2.Text = num1.ToString()
End If
Timer1.Enabled = True
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
count += 1
Timer1.Enabled = count <> 10
TB1.Text = count.ToString()
End Sub
End Class

How can i keep adding buttons related to what i wrote when i click the button

Public Class Form3
Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim oForm As Form2 = New Form2
Dim btn As Button = New Button With {
.Location = New Point(300, 100),
.Text = TextBox1.Text,
.ForeColor = Color.Black
}
oForm.Controls.Add(btn)
oForm.StartPosition = FormStartPosition.CenterScreen
oForm.Show(Me)
Me.Hide()
End Sub
End Class
I want to keep adding new buttons when the button is clicked but not at the same position i want the buttons to keep adding downwards like this:
[Button1]
[Button2]
[Button3]
You can get your Button1 location and than increase the Y value every time you click on it.
Public Class Form1
Dim Button1Coordinate As Point
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim btn As Button = New Button
Button1Coordinate.Y += 46
With btn
.Location = New Point(Button1Coordinate)
.Text = TextBox1.Text
.ForeColor = Color.Black
End With
Me.Controls.Add(btn)
Me.StartPosition = FormStartPosition.CenterScreen
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Button1Coordinate = Button1.Location
End Sub
End Class
N.B. the default height of a button is 23 pixels

Display text for drag and drop images VB.NET

I am developing a simple SQL generator for images. I am having issues getting texts to be displayed in a textbox when I drag pictures into a PictureBox. Am I doing anything wrong? I want a situation when I drag the image into the PictureBox, the textbox shown in blue should display: 'SELECT FROM EMPLOYEE;'. I need help to get this code working. My code is displayed below.
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'' picDropTarget.AllowDrop = True
picAccept.AllowDrop = True
End Sub
Private Sub picSELECT_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles picSELECT.MouseDown, picEMPLOYEE.MouseDown
' Start the drag if it's the left mouse button.
If (e.Button = MouseButtons.Left) Then
Dim source As PictureBox = DirectCast(sender, PictureBox)
picSELECT.DoDragDrop(source.Image, DragDropEffects.Copy)
'Added this line to show
'txtSQL.Text = "SELECT"
End If
End Sub
Private Sub picAccept_DragEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles picAccept.DragEnter
' See if this is a copy and the data includes an image.
If (e.Data.GetDataPresent(DataFormats.Bitmap) AndAlso
(e.AllowedEffect And DragDropEffects.Copy) <> 0) _
Then
' Allow this.
'e.Effect = DragDropEffects.All
e.Effect = DragDropEffects.All
Else
' Don't allow any other drop.
' e.Effect = DragDropEffects.None
e.Effect = DragDropEffects.Copy
End If
End Sub
Private Sub picAccept_DragDrop(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles picAccept.DragDrop, picSELECT.DragDrop, picEMPLOYEE.DragDrop
Dim bm As Bitmap = DirectCast(e.Data.GetData(DataFormats.Bitmap, True), Bitmap)
picAccept.Image = bm
End SubEnd Class
TextBox1.Text = "select from EMPLOYEES"
Here's a fully working minimal sample. You can copy/paste the below, drop a file into the PictureBox at the bottom of the form, and the TextBox at the top will get populated with text.
Public Class Form1
Dim txtbx As New TextBox
Dim pctbx As New PictureBox
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
txtbx.Dock = DockStyle.Top
pctbx.Dock = DockStyle.Bottom
pctbx.BackColor = Color.AntiqueWhite
pctbx.AllowDrop = True
Controls.Add(txtbx)
Controls.Add(pctbx)
AddHandler pctbx.DragEnter, AddressOf pctbx_DragEnter
AddHandler pctbx.DragDrop, AddressOf pctbx_DragDrop
End Sub
Private Sub pctbx_DragEnter(sender As Object, e As DragEventArgs)
e.Effect = DragDropEffects.All
End Sub
Private Sub pctbx_DragDrop(sender As Object, e As DragEventArgs)
txtbx.Text = "select from employees where id = "
End Sub
End Class

Visual Basic GDI+ Drawing Lines

I am working on a simple VB interface that should allow the user to draw a line from the point the mouse is clicked down to whereever the mouse position is, then leave the line where the mouse button is released. I am new to VB GDI+ and could use some pointers or tips. I need it so that I can draw multiple lines on the same control panel and everytime I start a new line somehow make it that the Paint paint function does not have to loop through each line I already drew. Here is my Code:
Public Class Form1
Dim down = False
Dim ptA As Point
Dim ptB As Point
Dim ptC As Point
Dim ptStart As New List(Of Point) ' Starting point of each line
Dim ptEnd As New List(Of Point) ' Ending Point of Each line
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.DoubleBuffered = True
End Sub
Private Sub Win_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Win.MouseDown
down = True
ptA = New Point(e.X, e.Y)
End Sub
Private Sub Win_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Win.MouseMove
If down = True Then
ptB = New Point(e.X, e.Y)
Win.Invalidate()
End If
End Sub
Private Sub Win_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Win.MouseUp
down = False
ptC = New Point(e.X, e.Y)
ptStart.Add(ptA)
ptEnd.Add(ptC)
End Sub
Private Sub Win_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Win.Paint
e.Graphics.Clear(Color.Black)
e.Graphics.DrawLine(Pens.LawnGreen, ptA, ptB)
For i = 0 To ptStart.Count - 1
Win.CreateGraphics.DrawLine(Pens.LawnGreen, ptStart(i), ptEnd(i))
Next
End Sub
End Class
In order to keep my existing lines, I stored the points in a list and each time I move the mouse while drawing, I call the Paint function and each time it is called, it loops through each point and draws a line between them. Any suggestions would be greatly appreciated.
Thank you,
JD
** Edit
Public Class Form1
Dim down = False
Dim ptA As Point
Dim ptB As Point
Dim ptC As Point
Dim ptStart As New List(Of Point)
Dim ptEnd As New List(Of Point)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.DoubleBuffered = True
End Sub
Private Sub Win_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Win.MouseDown
down = True
ptA = New Point(e.X, e.Y)
End Sub
Private Sub Win_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Win.MouseMove
If down = True Then
ptB = New Point(e.X, e.Y)
Win.Invalidate()
End If
End Sub
Private Sub Win_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Win.MouseUp
down = False
ptC = New Point(e.X, e.Y)
ptStart.Add(ptA)
ptEnd.Add(ptC)
End Sub
Private Sub Win_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Win.Paint
e.Graphics.Clear(Color.Black)
e.Graphics.DrawLine(Pens.LawnGreen, ptA, ptB)
For i = 0 To ptStart.Count - 1
e.Graphics.DrawLine(Pens.LawnGreen, ptStart(i), ptEnd(i))
Next
End Sub
End Class