falkonidas
falkonidas

Reputation: 15

Drawing on grid board

Im doing conway's game of life program. My board is two dimensional array filled with objects stated as alive or not. Im drawing board with:

Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
    Dim cellSize As Size = New Size(10, 10)

    For x As Integer = 1 To board_width - 1
        For y As Integer = 1 To board_height - 1

            Dim cellLocation As Point = New Point(x * cellSize.Width - 10, y * cellSize.Height - 10)

            Dim cell As Rectangle = New Rectangle(cellLocation, cellSize)

            Using cellBrush As SolidBrush = If(board(x, y).alive, New SolidBrush(Color.FromArgb(0, 0, 0)), New SolidBrush(Color.FromArgb(255, 255, 255)))
                e.Graphics.FillRectangle(cellBrush, cell)
            End Using
        Next
    Next
End Sub

Im changing state of one cell by clicking on board.

Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
    pic_pos.X = ((e.X - 5) / 10) + 1
    pic_pos.Y = ((e.Y - 5) / 10) + 1
    board(pic_pos.X, pic_pos.Y).alive = Not board(pic_pos.X, pic_pos.Y).alive
    PictureBox1.Refresh()
End Sub

Now i want to continuously change state of cells by clicking and holding mouse button(like drawing in paint) and i dont know completely how to do it. Any help or suggestions ? ps sorry for bad english

Upvotes: 1

Views: 145

Answers (1)

LarsTech
LarsTech

Reputation: 81675

Try using the MouseMove event, check for which button was pressed, and make sure your point is still within the bounds of your array:

Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles PictureBox1.MouseDown
  PictureBox1_MouseMove(sender, e)
End Sub

Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) Handles PictureBox1.MouseMove
  If e.Button = MouseButtons.Left Then
    pic_pos.X = ((e.X - 5) / 10) + 1
    pic_pos.Y = ((e.Y - 5) / 10) + 1
    If pic_pos.X >= 0 And pic_pos.X < board_width AndAlso _
       pic_pos.Y >= 0 And pic_pos.Y < board_width Then
      board(pic_pos.X, pic_pos.Y).alive = True
      PictureBox1.Invalidate()
    End If
  End If
End Sub

Upvotes: 1

Related Questions