-1

So my problem is that I want to have multiple rectangles on a form at a time. However I also want these rectangles to be able to be clicked and dragged across the form. This is my current code for clicking and dragging a rectangle that was drawn onto the form using the toolbox.

Public Class DragRectangle
Dim Go As Boolean
Dim LeftSet As Boolean
Dim TopSet As Boolean

Dim HoldLeft As Integer
Dim HoldTop As Integer

Dim OffLeft As Integer
Dim OffTop As Integer


Private Sub obj1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles RectangleShape1.MouseUp
    Go = False
    LeftSet = False
    TopSet = False
End Sub

Private Sub obj1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles RectangleShape1.MouseDown
    Go = True
End Sub

Private Sub obj1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles RectangleShape1.MouseMove
    If Go = True Then
        HoldLeft = (Control.MousePosition.X - Me.Left)
        HoldTop = (Control.MousePosition.Y - Me.Top)
        If TopSet = False Then
            OffTop = HoldTop - sender.Top
            TopSet = True
        End If
        If LeftSet = False Then
            OffLeft = HoldLeft - sender.Left
            LeftSet = True
        End If
        sender.Left = HoldLeft - OffLeft
        sender.Top = HoldTop - OffTop
    End If
End Sub
End Class

This works fine for one rectangle, although this requires the rectangles to be pre-drawn onto the form using the toolbox.

What I would like is a rectangle gets drawn by clicking a button on the form, and the newly drawn rectangle can also be clicked and dragged into a new location.

Is this possible? Thanks for any help

Navtik
  • 5
  • 1
  • 5

1 Answers1

0

Working example:

Public Class Form1
  Private Property Rectangles As New List(Of DrgRectangle)
  Private Property curRect As DrgRectangle
  Private _x As Integer
  Private _y As Integer
  Private Sub loadme() Handles Me.Load
   'load the rectangle in list
    Rectangles.Add(New DrgRectangle With {.Rect = New Rectangle(20, 20, 20, 20)})
  End Sub

  Private Sub FormMouseDown(sender As Object, e As MouseEventArgs) Handles Me.MouseDown
    _x = e.X
    _y = e.Y
    For Each rect In Rectangles
      If rect.Rect.Contains(e.X, e.Y) Then
        curRect = rect
        Exit For
      End If
    Next
  End Sub

  Private Sub FormMouseMove(sender As Object, e As MouseEventArgs) Handles Me.MouseMove
    If e.Button = Windows.Forms.MouseButtons.Left Then
      If curRect IsNot Nothing Then
        curRect.Rect = New Rectangle(New Point(curRect.Rect.Location.X + (e.X - _x), curRect.Rect.Location.Y + (e.Y - _y)), curRect.Rect.Size)
        Me.Refresh()
      End If
    End If
    _x = e.X
    _y = e.Y
  End Sub

  Private Sub FormPaint(sender As Object, e As PaintEventArgs) Handles me.Paint
    For Each rect In Rectangles
      e.Graphics.DrawRectangle(Pens.Black, rect.Rect)
    Next
  End Sub
End Class

Public Class DrgRectangle
  Public Rect As New Rectangle
  'add more properties as needed
End Class
OneFineDay
  • 9,004
  • 3
  • 26
  • 37