Imports System
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Collections
Imports System.ComponentModel
Imports System.Windows.Forms
Imports System.Data
Public Class frmMain
#Region "DECLARATIONS"
Private orignalStartPoint As Point 'Orignal Start point of the dragging
Private dragStartPoint As Point ' Present Start point of the dragging
Private darkPen As New Pen(SystemColors.ControlDark) ' Drawing Line Color
Private noTimesDragged As Integer
Private formSurface As Graphics
Private pt As Point
Private m_pt As Point
Private pnlSize As Size
Private pnl As Panel
'Private myPoint As Point
Private myPath As GraphicsPath
Private myRegion As Region
Private myTriangle As Point() = {New Point(pt.X, pt.Y), New Point(pnl.Location.X + 10, pnl.Location.Y + 10), New Point(pnl.Location.X + 10, pnl.Location.Y + 30)}
'****Added
Private trianglesByPanel As New Dictionary(Of Panel, Point)
#End Region
#Region "FORM EVENTS"
'Public Sub New()
' This call is required by the Windows Form Designer.
'InitializeComponent()
' Add any initialization after the InitializeComponent() call.
'Enable Double Buffering to remove Flickers
'Me.SetStyle(ControlStyles.AllPaintingInWmPaint, True)
'Me.SetStyle(ControlStyles.UserPaint, True)
'Me.SetStyle(ControlStyles.OptimizedDoubleBuffer, True)
'End Sub
'This Handels the form's drop event
Private Sub frmMain_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Me.DragDrop
If e.Data.GetDataPresent(GetType(Panel)) Then
pnl = DirectCast(e.Data.GetData(GetType(Panel)), Panel)
'Get the x and y screen positions after the drop of the label
Dim xOffset As Integer = e.X - Me.dragStartPoint.X
Dim yOffset As Integer = e.Y - Me.dragStartPoint.Y
'Drop the lable at this location
pnl.Location = New Point(pnl.Left + xOffset, pnl.Top + yOffset)
'*****Added
'myTriangle = Me.trianglesByPanel(pnl) '<--------------Problem here
drawTriangle()
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%INVALIDATE%%%%%%%%%%%%%%%%%%%%
'Me.Invalidate(myRegion)
End If
End Sub
'Handles the form's drag effects
Private Sub frmMain_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Me.DragEnter
If e.Data.GetDataPresent(GetType(Panel)) Then
e.Effect = DragDropEffects.Move
Else
e.Effect = DragDropEffects.None
End If
End Sub
Private Sub frmMain_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
pt = New Point(e.X, e.Y)
If e.Button = Windows.Forms.MouseButtons.Right Then
'Show the Context menu at the point when right mouse button is clicked
Me.ContextMenuStrip1.Show(Me, New System.Drawing.Point(e.X, e.Y))
End If
End Sub
'*****Added
Private Sub frmMain_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
'For Each key As Point In trianglesByPanel.Values
'Next
End Sub
#End Region
#Region "CONTROL EVENTS"
Private Sub cmnuDrawPanel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmnuDrawPanel.Click
pnl = New Panel
AddHandler pnl.MouseDown, AddressOf MyEventHandler
m_pt = New Point
m_pt.X = pt.X + 100
m_pt.Y = pt.Y + 100
pnl.Location = m_pt
pnlSize = New Size(200, 200)
pnl.Size = pnlSize
pnl.BorderStyle = BorderStyle.FixedSingle
Me.Controls.Add(pnl)
pnl.Name = "Panel1"
pnl.BackColor = Color.LightYellow
'Draw Triangle at first creation
drawTriangle()
'*****Add The Triangle to the Dictionery
trianglesByPanel.Add(pnl, myTriangle(0))
End Sub
#End Region
#Region "GENERAL FUNCTIONS"
Private Sub MyEventHandler(ByVal sender As Object, ByVal eMap As System.Windows.Forms.MouseEventArgs)
'cast object into a panel
pnl = CType(sender, Panel)
'get the start point of the label we are tryinng to drag, then start dragging the label
Me.dragStartPoint = pnl.PointToScreen(New Point(eMap.X, eMap.Y))
Me.orignalStartPoint = dragStartPoint
pnl.DoDragDrop(pnl, DragDropEffects.Move)
End Sub
Private Sub drawTriangle()
formSurface = Me.CreateGraphics
myPath = New GraphicsPath
myPath.AddPolygon(myTriangle)
'Draw the path to the screen.
Dim myPen As New Pen(Color.Black, 2)
formSurface.DrawPath(myPen, myPath)
myRegion = New Region(myPath)
formSurface.FillRegion(Brushes.LightYellow, myRegion)
End Sub
#End Region
End Class