Mouse Location + Amount IntersectsWith Button - vb.net

I have the cursor invisible and when a user scrolls over a button it highlights. The problem is when a user is in between buttons, he does not know where his cursor is.
On the mouse leave event I want the cursor to jump to the closest button (not the one he just left)
Private Sub btnNumbers_Mouseleave(sender As System.Object, e As System.EventArgs) Handles btnAlef.MouseLeave, btnBkspc.MouseLeave, btnBack.MouseLeave, btnClearAll.MouseLeave, btnDeleteWord.MouseLeave, btnEditMenu.MouseLeave, btnUndo.MouseLeave, btnSpeak.MouseLeave, btnGimel.MouseLeave, btnZayin.MouseLeave, btnYud.MouseLeave, btnVav.MouseLeave, btnTzadik.MouseLeave, btnTuf.MouseLeave, btnTes.MouseLeave, btnSpace.MouseLeave, btnShin.MouseLeave, btnSamech.MouseLeave, btnReish.MouseLeave, btnQuestion.MouseLeave, btnPred5.MouseLeave, btnPred4.MouseLeave, btnPred3.MouseLeave, btnPred2.MouseLeave, btnPred1.MouseLeave, btnPeriod.MouseLeave, btnPercent.MouseLeave, btnOpenParen.MouseLeave, btnNun.MouseLeave, btnMem.MouseLeave, btnLetterPrediction2.MouseLeave, btnLetterPrediction1.MouseLeave, btnLamed.MouseLeave, btnKuf.MouseLeave, btnHey.MouseLeave, btnFey.MouseLeave, btnExclamation.MouseLeave, btnEnter.MouseLeave, btnEnderTzadik.MouseLeave, btnEnderNun.MouseLeave, btnEnderMem.MouseLeave, btnEnderFey.MouseLeave, btnEnderChaf.MouseLeave, btnDollar.MouseLeave, btnDaled.MouseLeave, btnCloseParen.MouseLeave, btnChes.MouseLeave, btnChaf.MouseLeave, btnBkspc.MouseLeave, btnBeis.MouseLeave, btnAyin.MouseLeave, btnApostrophe.MouseLeave, btn9.MouseLeave, btn8.MouseLeave, btn7.MouseLeave, btn6.MouseLeave, btn5.MouseLeave, btn4.MouseLeave, btn3.MouseLeave, btn2.MouseLeave, btn1.MouseLeave, btn0.MouseLeave, btnSavedPhrases5.MouseLeave, btnSavedPhrases4.MouseLeave, btnSavedPhrases3.MouseLeave, btnSavedPhrases2.MouseLeave, btnSavedPhrases1.MouseLeave, btnSettings.MouseLeave, btnPhrases.MouseLeave, btnNumbers.MouseLeave, btnMinimize.MouseLeave, btnHebrew.MouseLeave, btnExit.MouseLeave, btnCopy.MouseLeave, btnRightWord.MouseLeave, btnRightChar.MouseLeave, btnLeftWord.MouseLeave, btnLeftChar.MouseLeave, btnHome.MouseLeave, btnEnd.MouseLeave, btnT8.MouseLeave, btnT7.MouseLeave, btnT6.MouseLeave, btnT5.MouseLeave, btnT4.MouseLeave, btnT3.MouseLeave, btnT2.MouseLeave, btnT1.MouseLeave
Dim btn As Button = DirectCast(sender, Button)
btn.FlatStyle = FlatStyle.Standard
Dim GetCursorPos = Cursor.Position
If FormSettings.chbxBorderHover.Checked = True Then
For Each c As Control In Me.Controls.OfType(Of Button)()
If GetCursorPos.IntersectsWith(c.Bounds) Then
Cursor.Position = c.Location
Exit For
End If
Next
End If
End Sub
This is what I have so far, however intercectsWith does not work with a drawing point
Is there any way for my to check what the closest button is?

I think the interface will be hard to use (especially if the buttons are not large enough), but here's another implementation:
Private Sub btnNumbers_Mouseleave(sender As System.Object, e As System.EventArgs) Handles btnAlef.MouseLeave, btnBkspc.MouseLeave, btnBack.MouseLeave, btnClearAll.MouseLeave, btnDeleteWord.MouseLeave, btnEditMenu.MouseLeave, btnUndo.MouseLeave, btnSpeak.MouseLeave, btnGimel.MouseLeave, btnZayin.MouseLeave, btnYud.MouseLeave, btnVav.MouseLeave, btnTzadik.MouseLeave, btnTuf.MouseLeave, btnTes.MouseLeave, btnSpace.MouseLeave, btnShin.MouseLeave, btnSamech.MouseLeave, btnReish.MouseLeave, btnQuestion.MouseLeave, btnPred5.MouseLeave, btnPred4.MouseLeave, btnPred3.MouseLeave, btnPred2.MouseLeave, btnPred1.MouseLeave, btnPeriod.MouseLeave, btnPercent.MouseLeave, btnOpenParen.MouseLeave, btnNun.MouseLeave, btnMem.MouseLeave, btnLetterPrediction2.MouseLeave, btnLetterPrediction1.MouseLeave, btnLamed.MouseLeave, btnKuf.MouseLeave, btnHey.MouseLeave, btnFey.MouseLeave, btnExclamation.MouseLeave, btnEnter.MouseLeave, btnEnderTzadik.MouseLeave, btnEnderNun.MouseLeave, btnEnderMem.MouseLeave, btnEnderFey.MouseLeave, btnEnderChaf.MouseLeave, btnDollar.MouseLeave, btnDaled.MouseLeave, btnCloseParen.MouseLeave, btnChes.MouseLeave, btnChaf.MouseLeave, btnBkspc.MouseLeave, btnBeis.MouseLeave, btnAyin.MouseLeave, btnApostrophe.MouseLeave, btn9.MouseLeave, btn8.MouseLeave, btn7.MouseLeave, btn6.MouseLeave, btn5.MouseLeave, btn4.MouseLeave, btn3.MouseLeave, btn2.MouseLeave, btn1.MouseLeave, btn0.MouseLeave, btnSavedPhrases5.MouseLeave, btnSavedPhrases4.MouseLeave, btnSavedPhrases3.MouseLeave, btnSavedPhrases2.MouseLeave, btnSavedPhrases1.MouseLeave, btnSettings.MouseLeave, btnPhrases.MouseLeave, btnNumbers.MouseLeave, btnMinimize.MouseLeave, btnHebrew.MouseLeave, btnExit.MouseLeave, btnCopy.MouseLeave, btnRightWord.MouseLeave, btnRightChar.MouseLeave, btnLeftWord.MouseLeave, btnLeftChar.MouseLeave, btnHome.MouseLeave, btnEnd.MouseLeave, btnT8.MouseLeave, btnT7.MouseLeave, btnT6.MouseLeave, btnT5.MouseLeave, btnT4.MouseLeave, btnT3.MouseLeave, btnT2.MouseLeave, btnT1.MouseLeave
Dim btn As Button = DirectCast(sender, Button)
btn.FlatStyle = FlatStyle.Standard
If FormSettings.chbxBorderHover.Checked = True Then
Dim currentPos As Point = Me.PointToClient(Cursor.Position)
Dim closestButton = (From x In Me.Controls.OfType(Of Button)()
Where x IsNot btn
Order By PointToButtonDistance(currentPos, x) Ascending).FirstOrDefault
Cursor.Position = closestButton.PointToScreen(New Point(0, 0))
End If
End Sub
Private Function PointToButtonDistance(ByVal pt As Point, ByVal btn As Button) As Long
Dim center As New Point(btn.Location.X + btn.Width / 2, btn.Location.Y + btn.Height / 2)
Dim dx As Integer = Math.Max(Math.Abs(pt.X - center.X) - btn.Width / 2, 0)
Dim dy As Integer = Math.Max(Math.Abs(pt.Y - center.Y) - btn.Height / 2, 0)
Return dx * dx + dy * dy
End Function

Dim minDist As Double = Double.MaxVal
Dim closestCtrl as Control = Nothing
For Each c As Control In Me.Controls.OfType(Of Button)()
If c Is sender Then Continue For ' Skip the one we came from
Dim dist = ComputeDistance(Cursor.Position, c.Bounds)
If dist < minDist Then
minDist = dist
closestCtrl = c
End If
Next
...
' Compute distance between 2 points
Private Function ComputeDistance(x1 as Integer, y1 as Integer, x2 as Integer, y2 as Integer) as Double
Dim dx = (x1 - x2)
Dim dy = (y1 - y2)
Return Math.Sqrt(dx*dx + dy*dy)
End Function
' Compute distance between a point and the closest corner of a rectangle
' I'm going to do this a lazy way. There's probably a better one
Private Function ComputeDistance(pt as Point, rect as Rectangle) as Distance
Dim dist as New List(of Double)
dist.Add(ComputeDistance(pt.x, pt.y, rect.Left, rect.Top)
dist.Add(ComputeDistance(pt.x, pt.y, rect.Right, rect.Top)
dist.Add(ComputeDistance(pt.x, pt.y, rect.Left, rect.Bottom)
dist.Add(ComputeDistance(pt.x, pt.y, rect.Right, rect.Bottom)
Return dist.Min()
End Function

Related

Is there a way to convert Pixel coordinates to Cartesian Coordinates in VB.net

I have a PictureBox that is sized 1096 x 1004 with the SizeMode set to StretchImage. I am able to get the coordinates of each pixel correctly(see code below) by factoring in the StrechImage effect on the pixel coordinates.
Now what I am trying to accomplish is converting those pixel coordinates to a Cartesian Coordinate to be able to graph. In the long run, I am going to take the Cartesian Coordinates and convert them to Polar Coordinates.
I have tried to convert the pixel coordinates to cartesian by using this method.
cartesianx = scalefactor*screenx - screenwidth / 2;
cartesiany = -scalefactor*screeny + screenheight / 2;
This method is not putting the origin at (0,0) in the center of the PictureBox. It seems to be setting the origin closer to the Upper Left of the PictureBox. Is there any idea as to what I am missing?
Below is my code to convert the image to BitMap and get those coordinates and scale them correctly.
Imports System.IO
Public Class HomePanel
Dim realX As Int32
Dim realY As Int32
Private Sub HomePanel_Load(sender As Object, e As EventArgs) Handles MyBase.Load
chartImageDisplay_box.Image = Image.FromFile("C:\Users\UserB\Desktop\test.jpg")
End Sub
Private Sub chartImageDisplay_box_MouseMove(sender As Object, e As MouseEventArgs) Handles chartImageDisplay_box.MouseMove
If (e.Button = MouseButtons.Left) Then
ShowCoords(e.X, e.Y)
End If
End Sub
Private Sub chartImageDisplay_box_MouseDown(sender As Object, e As MouseEventArgs) Handles chartImageDisplay_box.MouseDown
If (e.Button = MouseButtons.Left) Then
ShowCoords(e.X, e.Y)
Dim MyBitmap As Bitmap
MyBitmap = CType(chartImageDisplay_box.Image, Bitmap)
'Me.BackColor = MyBitmap.GetPixel(realX, realY)
rgbValue.Text = "RGB Value: " & MyBitmap.GetPixel(realX, realY).ToString()
End If
'printAllPixels()
End Sub
Private Sub ShowCoords(ByVal mouseX As Int32, ByVal mouseY As Int32)
Dim realW As Int32 = chartImageDisplay_box.Image.Width
Dim realH As Int32 = chartImageDisplay_box.Image.Height
Dim currentW As Int32 = chartImageDisplay_box.ClientRectangle.Width
Dim currentH As Int32 = chartImageDisplay_box.ClientRectangle.Height
Dim zoomW As Double = (currentW / CType(realW, Double))
Dim zoomH As Double = (currentH / CType(realH, Double))
Dim zoomActual As Double = Math.Min(zoomW, zoomH)
Dim padX As Double = If(zoomActual = zoomW, 0, (currentW - (zoomActual * realW)) / 2)
Dim padY As Double = If(zoomActual = zoomH, 0, (currentH - (zoomActual * realH)) / 2)
realX = CType(((mouseX - padX) / zoomActual), Int32)
realY = CType(((mouseY - padY) / zoomActual), Int32)
lblPosXval.Text = "X: " & If(realX < 0 OrElse realX > realW, "-", realX.ToString())
lblPosYVal.Text = "Y: " & If(realY < 0 OrElse realY > realH, "-", realY.ToString())
cartX.Text = "X: " 'Where to add the cart conversion for X
cartY.Text = "Y: " 'Where to add the cart conversion for Y
End Sub
'Writes all the pixels to a text file along with RGB values for each pixel
Public Sub printAllPixels()
Using writer As StreamWriter =
New StreamWriter("C:\Users\UserB\Desktop\Pixels.txt")
Dim MyBitmap As Bitmap
MyBitmap = CType(chartImageDisplay_box.Image, Bitmap)
For y = 0 To MyBitmap.Height - 1
For x = 0 To MyBitmap.Width - 1
writer.WriteLine("XY Coord: " & x & ", " & y & "; " & MyBitmap.GetPixel(x, y).ToString)
Next
Next
End Using
End Sub
End Class
I don't know if the content of the variable contains the right value but the formula should look more like this:
cartesianx = scalefactor * (screenx - (screenwidth / 2))
cartesiany = -scalefactor* (screeny - (screenheight / 2))
Translate to 0,0 add the scale factor then flip the y.
I believe I figured my question out. I was using the wrong value for my screenx and screeny. I was using the calculated scale value but I needed to just use the mouse event X and Y values.

Giving Dynamically Created Shapes a Name

I'm designing a hexagon grid and I need to be able to name each hexagon, so I can refer to them later. Below is my class, it generates the hexagon grid, and I've labeled the code throughout so you can understand what's happening.
I've been searching for a while now reading a lot about Graphics, but I can't get a working design with the answers I've seen offered. Perhaps, I'm going about this wrong by using Graphics, but my plan is to be able to click on each hexagon and do something with it.
Note: If you see a way to improve my code let me know. It's appreciated!
' Generate Hexagon Grid
Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
' Hexagon Grid Parameters
Dim HexagonRadius As Integer = 20 ' Fix "Position Hexagon Grid Columns" Before Changing Hexagon Radius
Dim GridSize As Integer = 10
' Generate Hexagon Grid
Dim HexagonX As Integer = HexagonRadius
Dim HexagonY As Integer = HexagonRadius
For i As Integer = 1 To GridSize
For j As Integer = 1 To GridSize
' Hexagon Vertex Coordinates
Dim point1 As New Point((HexagonX - HexagonRadius), (HexagonY))
Dim point2 As New Point((HexagonX - (HexagonRadius / 2)), (HexagonY + ((HexagonRadius / 2) * Math.Sqrt(3))))
Dim point3 As New Point((HexagonX + (HexagonRadius / 2)), (HexagonY + ((HexagonRadius / 2) * Math.Sqrt(3))))
Dim point4 As New Point((HexagonX + HexagonRadius), (HexagonY))
Dim point5 As New Point((HexagonX + (HexagonRadius / 2)), (HexagonY - ((HexagonRadius / 2) * Math.Sqrt(3))))
Dim point6 As New Point((HexagonX - (HexagonRadius / 2)), (HexagonY - ((HexagonRadius / 2) * Math.Sqrt(3))))
Dim hexagonPoints As Point() = {point1, point2, point3, point4, point5, point6}
' Create Hexagon
e.Graphics.FillPolygon(Brushes.Green, hexagonPoints)
' Hexagon Outline
e.Graphics.DrawLine(Pens.Black, point1, point2)
e.Graphics.DrawLine(Pens.Black, point2, point3)
e.Graphics.DrawLine(Pens.Black, point3, point4)
e.Graphics.DrawLine(Pens.Black, point4, point5)
e.Graphics.DrawLine(Pens.Black, point5, point6)
e.Graphics.DrawLine(Pens.Black, point6, point1)
' Position Hexagon Grid Columns
HexagonY += 34 ' Specific to Hexagon Radius: 20
Next
If i Mod 2 > 0 Then
HexagonY = 36.75 ' Specific to Hexagon Radius: 20
Else
HexagonY = 20 ' Specific to Hexagon Radius: 20
End If
HexagonX += 30 ' Specific to Hexagon Radius: 20
Next
End Sub
You'll need to create some Hexagon class with it's coordinates and (maybe name, if really needed). And save them to some suitable collection (2-dimensional array maybe?)
This should happen somewhere outside your Paint event and might be recalculated on grid SizeChanged event.
Inside your Paint event you'll just iterate throught existing collection and render according to pre-computed coordinates.
OnClick event will loop throught the same collection to find specific Hexagon for updating (changing background color for example) and forcing form to repaint to take effect.
For large rendering you should consider rendering to bitmap first and drawing that final bitmap to e.Graphics for faster work. Your bitmap could be cached as well to speed up even more.
EDIT: Code sample added
Turn Option Strict On in your project properties to avoid many problems in your code that you're not aware of.
Public Class frmTest
Private Const HexagonRadius As Integer = 20
Private Const GridSize As Integer = 10
Private fHexagons As New List(Of Hexagon)
Private fCache As Bitmap
Private fGraphics As Graphics
Private Sub ResetHexagons() 'Call when some parameter changes (Radius/GridSize)
fHexagons.Clear()
Invalidate()
End Sub
Private Function EnsureHexagons() As List(Of Hexagon)
Dim X, Y As Single, xi, yi As Integer
If fHexagons.Count = 0 Then
X = HexagonRadius : Y = HexagonRadius
For xi = 1 To GridSize
For yi = 1 To GridSize
fHexagons.Add(New Hexagon(HexagonRadius, X, Y))
Y += 34
Next
'Do your math to get theese values from HexagonRadius value
If xi Mod 2 > 0 Then
Y = 36.75
Else
Y = 20
End If
X += 30
Next
fCache?.Dispose()
fGraphics?.Dispose()
fCache = New Bitmap(GridSize * HexagonRadius * 2, GridSize * HexagonRadius * 2)
fGraphics = Graphics.FromImage(fCache)
For Each H As Hexagon In fHexagons
H.Render(fGraphics)
Next
End If
Return fHexagons
End Function
Private Sub frmTest_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
EnsureHexagons()
e.Graphics.DrawImageUnscaled(fCache, Point.Empty)
End Sub
Private Sub frmTest_MouseClick(sender As Object, e As MouseEventArgs) Handles Me.MouseClick
Dim H As Hexagon = EnsureHexagons.FirstOrDefault(Function(X) X.Contains(e.Location))
If H IsNot Nothing Then
H.Checked = Not H.Checked
H.Render(fGraphics) 'Update cache without repainting all
Invalidate()
End If
End Sub
End Class
Public Class Hexagon
Public ReadOnly Radius, X, Y As Single
Public ReadOnly Points() As PointF
Public Property Checked As Boolean
Public Sub New(Radius As Single, X As Single, Y As Single)
Me.Radius = Radius : Me.X = X : Me.Y = Y
Points = {New PointF((X - Radius), (Y)),
New PointF((X - (Radius / 2)), CSng(Y + ((Radius / 2) * Math.Sqrt(3)))),
New PointF((X + (Radius / 2)), CSng(Y + ((Radius / 2) * Math.Sqrt(3)))),
New PointF((X + Radius), (Y)),
New PointF((X + (Radius / 2)), CSng(Y - ((Radius / 2) * Math.Sqrt(3)))),
New PointF((X - (Radius / 2)), CSng(Y - ((Radius / 2) * Math.Sqrt(3.0!))))}
End Sub
Public Sub Render(G As Graphics)
' Create Hexagon
G.FillPolygon(If(Checked, Brushes.Blue, Brushes.Green), Points)
' Hexagon Outline
For i As Integer = 0 To Points.Length - 1
G.DrawLine(Pens.Black, Points(i), Points((i + 1) Mod Points.Length))
Next
End Sub
Public Function Contains(P As Point) As Boolean
'Do your math here, this is just simplified estimation
Return X - Radius <= P.X AndAlso P.X <= X + Radius AndAlso Y - Radius <= P.Y AndAlso P.Y <= Y + Radius
End Function
End Class

Snake Game... Adding snake body function malfunctioning

I am writing a snake game in visual studio in visual basic.
The playing field is a 2D Array of PictureBoxes. My Snake is a 1D array as type Point. The snake array is called 'Snake'.
When the form loads, Snake(0) is set as New Point(1, 1). I have created a sub routine that moves the snake depending on the arrow key the user presses. This is under a timer. Snake(0) (The snake head) is set to equal Snake(0) + direction (direction is a variable altered by the arrow key that the user presses, eg. when up is pressed direction is set to x: 0 and y: -1)
When snake(0) hits a piece of food, the amount of elements in the snake array is set to the length of the array. EG(If snake(0) = foodPosition Then ReDim Preserve snake(snake.Length) End If)
I have created a loop, also under the timer, to make the body of the snake follow the head (eg. snake(2) = snake(1) and snake(1) = snake(0) but can't get it to work)
Code:
Public Class frmPlayfield
'Food Creating and Grow Snake Variables
Dim randF As New Random
Dim foodPointX As Integer = randF.Next(0, 32)
Dim foodPointY As Integer = randF.Next(0, 32)
'Play Field Variables
Dim playMaxWidth As Integer = 32
Dim playMaxHeight As Integer = 32
Dim boxSize As Integer = 16 'Size of PictureBox
Dim boxArray(,) As PictureBox 'PictureBox Array
'Snake Stuff Variable
Dim snake(1) As Point 'Snake array
Dim direction As New Point(1, 0) 'Direction for snake movement
Private Sub frmPlayfield_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ReDim boxArray(playMaxWidth, playMaxHeight)
For x As Integer = 0 To playMaxWidth
For y As Integer = 0 To playMaxHeight
boxArray(x, y) = New PictureBox
boxArray(x, y).Width = boxSize
boxArray(x, y).Height = boxSize
boxArray(x, y).Top = y * boxSize
boxArray(x, y).Left = x * boxSize
boxArray(x, y).Visible = True
boxArray(x, y).BackColor = Color.White
boxArray(x, y).BorderStyle = BorderStyle.FixedSingle
Me.Controls.Add(boxArray(x, y))
Next
Next
Me.ClientSize = New Size((playMaxWidth + 1) * boxSize, (playMaxHeight + 1) * boxSize)
snake(0) = New Point(1, 1) 'Creates snake head
boxArray(foodPointX, foodPointY).BackColor = Color.Red
End Sub
Private Function createBox(x As Integer, y As Integer, bSize As Integer) As PictureBox
Dim tempBox As New PictureBox
tempBox.Width = bSize
tempBox.Height = bSize
tempBox.Top = y * bSize
tempBox.Left = x * bSize
tempBox.Visible = True
tempBox.BackColor = Color.White
tempBox.BorderStyle = BorderStyle.FixedSingle
Me.Controls.Add(tempBox)
Return tempBox
End Function
Private Sub Food()
If snake(0).X = foodPointX And snake(0).Y = foodPointY Then
ReDim Preserve snake(snake.Length) 'Increases the amount of elements in the snake array.
For j As Integer = 0 To 0
foodPointX = randF.Next(0, 32)
foodPointY = randF.Next(0, 32)
boxArray(foodPointX, foodPointY).BackColor = Color.Red
Next
End If
For h As Integer = snake.Length - 1 To snake.GetUpperBound(0)
snake(h) = snake(snake.Length - 2)
Next
End Sub
Private Sub CheckBoundsAndMovement()
For i As Integer = 0 To snake.GetUpperBound(0)
boxArray(snake(i).X, snake(i).Y).BackColor = Color.White 'Loop to change the whole snake black
Next
snake(1) = snake(0)
snake(0) = snake(0) + direction
If snake(0).X > playMaxWidth Then
snake(0).X -= (playMaxWidth + 1)
End If
If snake(0).X < 0 Then
snake(0).X += (playMaxWidth + 1)
End If 'Four If statements to check if the snake has gone outside the play area.
If snake(0).Y > playMaxWidth Then
snake(0).Y -= (playMaxWidth + 1)
End If
If snake(0).Y < 0 Then
snake(0).Y += (playMaxWidth + 1)
End If
For k As Integer = 0 To snake.GetUpperBound(0)
boxArray(snake(k).X, snake(k).Y).BackColor = Color.Black 'Loop to make the whole snake black
Next
End Sub
Private Sub timGameTick_Tick(sender As Object, e As EventArgs) Handles timGameTick.Tick
Food()
CheckBoundsAndMovement()
End Sub
Private Sub frmPlayfield_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown 'Subroutine for direction
Select Case (e.KeyCode)
Case Keys.Up
direction = New Point(0, -1)
Case Keys.Down
direction = New Point(0, 1)
Case Keys.Left
direction = New Point(-1, 0)
Case Keys.Right
direction = New Point(1, 0)
End Select
End Sub
End Class
This works fine after I eat the first piece of food. The snake of length 2 is increased to length 3. But when I eat another piece of food the end of the snake is left behind at the spot where the food was eaten.
Ok - It looks like I found the problems -
First off - you defined the snake array as Dim snake (1) As Point this created the array with two elements instead of 1
Next, in your Food sub, the loop to roll the pixels back along the snake should be done every time the snake moves, not just when food is eaten. So I moved it into the CheckBoundsAndMovement sub to replace line 4 of that sub which only copied the location of the head to the next point back rather than the whole snake. But of course trying to execute the loop when the length of the snake was only one pixel would result in an out of range exception on the array, so added an If statement to only execute the loop if the length of snake is more than 1.
Also the direction of the loop in your code was in increasing order. To do it properly it should be in decreasing order. This way, the loop overwrites the point representing the end of the tail with the next point forward and so on. Finally, the new location for the head is entered into snake(0)
So - here it is -
Public Class frmPlayfield
'Food Creating and Grow Snake Variables
Dim randF As New Random
Dim foodPointX As Integer = randF.Next(0, 32)
Dim foodPointY As Integer = randF.Next(0, 32)
'Play Field Variables
Dim playMaxWidth As Integer = 32
Dim playMaxHeight As Integer = 32
Dim boxSize As Integer = 16 'Size of PictureBox
Dim boxArray(,) As PictureBox 'PictureBox Array
'Snake Stuff Variable
Dim snake(0) As Point 'Snake array
Dim direction As New Point(1, 0) 'Direction for snake movement
Private Sub frmPlayfield_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ReDim boxArray(playMaxWidth, playMaxHeight)
For x As Integer = 0 To playMaxWidth
For y As Integer = 0 To playMaxHeight
boxArray(x, y) = New PictureBox
boxArray(x, y).Width = boxSize
boxArray(x, y).Height = boxSize
boxArray(x, y).Top = y * boxSize
boxArray(x, y).Left = x * boxSize
boxArray(x, y).Visible = True
boxArray(x, y).BackColor = Color.White
boxArray(x, y).BorderStyle = BorderStyle.FixedSingle
Me.Controls.Add(boxArray(x, y))
Next
Next
Me.ClientSize = New Size((playMaxWidth + 1) * boxSize, (playMaxHeight + 1) * boxSize)
snake(0) = New Point(1, 1) 'Creates snake head
boxArray(foodPointX, foodPointY).BackColor = Color.Red
End Sub
Private Function createBox(x As Integer, y As Integer, bSize As Integer) As PictureBox
Dim tempBox As New PictureBox
tempBox.Width = bSize
tempBox.Height = bSize
tempBox.Top = y * bSize
tempBox.Left = x * bSize
tempBox.Visible = True
tempBox.BackColor = Color.White
tempBox.BorderStyle = BorderStyle.FixedSingle
Me.Controls.Add(tempBox)
Return tempBox
End Function
Private Sub Food()
If snake(0).X = foodPointX And snake(0).Y = foodPointY Then
ReDim Preserve snake(snake.Length)
foodPointX = randF.Next(0, 32)
foodPointY = randF.Next(0, 32)
boxArray(foodPointX, foodPointY).BackColor = Color.Red
End If
End Sub
Private Sub CheckBoundsAndMovement()
For i As Integer = 0 To snake.GetUpperBound(0)
boxArray(snake(i).X, snake(i).Y).BackColor = Color.White 'Loop to change the whole snake white
boxArray(snake(i).X, snake(i).Y).Update()
Next
If snake.Length > 1 Then
For i As Integer = snake.GetUpperBound(0) To 1 Step -1
snake(i) = snake(i - 1)
Next
End If
snake(0) = snake(0) + direction
If snake(0).X > playMaxWidth Then
snake(0).X -= (playMaxWidth + 1)
End If
If snake(0).X < 0 Then
snake(0).X += (playMaxWidth + 1)
End If 'Four If statements to check if the snake has gone outside the play area.
If snake(0).Y > playMaxWidth Then
snake(0).Y -= (playMaxWidth + 1)
End If
If snake(0).Y < 0 Then
snake(0).Y += (playMaxWidth + 1)
End If
For k As Integer = 0 To snake.GetUpperBound(0)
boxArray(snake(k).X, snake(k).Y).BackColor = Color.Black 'Loop to make the whole snake black
Next
End Sub
Private Sub timGameTick_Tick(sender As Object, e As EventArgs) Handles timGameTick.Tick
Food()
CheckBoundsAndMovement()
End Sub
Private Sub frmPlayfield_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown 'Subroutine for direction
Select Case (e.KeyCode)
Case Keys.Up
direction = New Point(0, -1)
Case Keys.Down
direction = New Point(0, 1)
Case Keys.Left
direction = New Point(-1, 0)
Case Keys.Right
direction = New Point(1, 0)
End Select
End Sub
End Class

Determine tablelayoutpanel item dropped into

I am trying to determine which cell (row/column) of my TableLayoutPanel the user drops an object into. Currently I have only been able to find how to determine coordinates of where the item is dropped which is:
Dim location As Point = TableLayoutPanel1.PointToClient(New Point(e.X, e.Y))
However I can not figure out how to locate which cell that is in. I did find the command GetCellPosition and attempted that with the coordinates; however that did not work either.
TableLayoutPanel1.GetCellPosition(location)
You can try this function:
Private Function GetCellFromPoint(p As Point) As Point
Dim result As New Point(-1, -1)
Dim colWidths As Integer() = tlp.GetColumnWidths()
Dim rowHeights As Integer() = tlp.GetRowHeights()
Dim top As Integer = 0
For y As Integer = 0 To rowHeights.Length - 1
Dim left As Integer = 0
For x As Integer = 0 To colWidths.Length - 1
If New Rectangle(left, top, colWidths(x), rowHeights(y)).Contains(p) Then
result = New Point(x, y)
End If
left += colWidths(x)
Next
top += rowHeights(y)
Next
Return result
End Function
It just loops through the rows and columns to see if the passed in point is inside the existing cell. Note though, that GetColumnWidths and GetRowHeights do not appear in the intellisense dropdown.
Usage:
Private Sub tlp_MouseMove(sender As Object, e As MouseEventArgs) _
Handles tlp.MouseMove
Me.Text = GetCellFromPoint(e.Location).ToString
End Sub
BTW, GetCellPosition is expecting a control to be passed as a parameter, not a Point structure.
You can also use TableLayoutPanelCellPosition in place of Point in this function, since that is what GetCellPosition is returning in its function.
I have used this function (thanks a million) but I have found an issue. To define correctly the cell where the object is droped, we need to take into account the location of the TableLayoutPanel in the screen. I have solved this making the declaration of p (build with e.X and e.Y in the DragDrop event) and r (a reference of the location of the TableLayoutPanel in the screen).
Then you have to assing p = p - r and send that P to the function GetCellFromPoint(p).
Private Sub TableLayoutPanel1_DragDrop(sender As Object, e As DragEventArgs) Handles TableLayoutPanel1.DragDrop
Dim p As New Point(e.X, e.Y)
Dim r As Point
r = TableLayoutPanel1.PointToScreen(New Point(0, 0))
p.X = p.X - r.X
p.Y = p.Y - r.Y
MessageBox.Show(GetCellFromPoint(p).ToString)
End Sub

Runtime error message Index was outside the bounds of the array. for Visual Basic 2010

I am computing the ROI with a moving rectangle and extracting the ROI to compute the standard deviation, mean, area and Pixel value coordinates X and Y in a seperate form2 by clicking the mouse. At this juncture I am trying to pass a function from the main Form that loads the Image and displays the rectangle to another Form that has the displayed properties of the mean and standard deviation etc. However, I'm receiving errors in runtime in the function that contains the standard deviation. The error displayed is
Index was outside the bounds of the array.
It is displayed at the end of this portion of the code in the function StD, i.e at the end of the mean part'
SD(count) = Double.Parse(pixelcolor.R) + Double.Parse(pixelcolor.G) + Double.Parse(pixelcolor.B) - mean
what is this actually saying and how can I fix this situation. Any tips and ideas, thanks.
My code is at the bottom
enterPublic Function StD(ByVal image As Bitmap, ByVal mean As Double, ByVal meancount As Integer) As Double
Dim SD(SquareHeight * SquareWidth) As Double
Dim count As Integer = 0
For i = 0 To SquareWidth
For j = 0 To SquareHeight
Dim pixelcolor As Color = image.GetPixel(i, j)
SD(count) = Double.Parse(pixelcolor.R) + Double.Parse(pixelcolor.G) + Double.Parse(pixelcolor.B) - mean
count += 1
Next
Next
Dim SDsum As Double = 0
For i = 0 To count
SDsum = SDsum + SD(i)
Next
SDsum = SDsum / (SquareHeight * SquareWidth)
SDsum = ((SDsum) ^ (1 / 2))
Return SDsum
End Function code here
I would like to pass this using the code below
enterPrivate Sub PictureBox1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
Dim mean As Double = 0
Dim meancount As Integer = 0
Dim bmap As New Bitmap(400, 400)
bmap = PictureBox1.Image
Dim colorpixel As Color = bmap.GetPixel(e.X, e.Y)
' Dim pixels As Double = colorpixel.R + colorpixel.G + colorpixel.B
If e.Button = Windows.Forms.MouseButtons.Left AndAlso Rect.Contains(e.Location) Then
If (PictureBox1.Image Is Nothing) Or (PictureBox1.Height - (e.Y + SquareHeight) < 0) Or (PictureBox1.Width - (e.X + SquareWidth) < 0) Then
Else
Dim ROI As New Bitmap(400, 400)
Dim x As Integer = 0
Dim countx As Integer = 0
Dim county As Integer = 0
For i = e.X To (e.X + SquareWidth)
For j = (e.Y + x) To (e.Y + SquareHeight)
Dim pixelcolor As Color = bmap.GetPixel(i, j)
ROI.SetPixel(countx, county, pixelcolor)
mean = mean + pixelcolor.R + pixelcolor.G + pixelcolor.B
county += 1
meancount += 1
Next
county = 0
countx += 1
x = x + 1
Next
mean = mean / (meancount * 3)
Dim SD = mean - 75
Dim area As Integer = (SquareHeight * SquareWidth)
Dim anotherForm As Form2
anotherForm = New Form2(mean, StD(bmap, mean, meancount), area, 34)
anotherForm.Show()
End If
End If
' Catch ex As Exception
' MessageBox.Show(ex.Message())
' End Try
End Sub code here
To be displayed with this code
enter Public Sub New(ByVal mean As Double, ByVal StD As Double, ByVal Area As Integer, ByVal pixel As Double)
MyBase.New()
InitializeComponent()
TextBox1.Text = mean.ToString()
TextBox2.Text = StD.ToString()
TextBox3.Text = Area.ToString()
TextBox4.Text = pixel.ToString()
End Sub code here
The problem probably is because of these lines:
For i = 0 To SquareWidth
For j = 0 To SquareHeight
Try using this instead:
For i = 0 To SquareWidth - 1
For j = 0 To SquareHeight - 1