Imports Microsoft.VisualBasic Public Class ClassSnapPanel Inherits System.Windows.Forms.Panel Private _ShowGrid As Boolean = True Private _GridSize As Integer = 16 Private Property AutoScaleMode As AutoScaleMode Public Property GridSize As Integer Get Return _GridSize End Get Set(value As Integer) _GridSize = value Refresh() End Set End Property Public Property ShowGrid As Boolean Get Return _ShowGrid End Get Set(value As Boolean) _ShowGrid = value Refresh() End Set End Property Protected Overrides Sub OnControlAdded(e As System.Windows.Forms.ControlEventArgs) AddHandler e.Control.LocationChanged, AddressOf AlignToGrid AddHandler e.Control.DragDrop, AddressOf AlignToGrid MyBase.OnControlAdded(e) End Sub Protected Overrides Sub OnControlRemoved(e As System.Windows.Forms.ControlEventArgs) RemoveHandler e.Control.LocationChanged, AddressOf AlignToGrid RemoveHandler e.Control.DragDrop, AddressOf AlignToGrid MyBase.OnControlRemoved(e) End Sub Protected Overrides Sub OnPaint(e As System.Windows.Forms.PaintEventArgs) If _ShowGrid Then ControlPaint.DrawGrid(e.Graphics, ClientRectangle, New Size(_GridSize, _GridSize), BackColor) End If MyBase.OnPaint(e) End Sub Private Sub AlignToGrid(sender As Object, e As EventArgs) If _ShowGrid Then Dim item As Control = CType(sender, Control) Dim x As Integer = Math.Round(item.Left / _GridSize) * _GridSize Dim y As Integer = Math.Round(item.Top / _GridSize) * _GridSize item.Location = New Point(x, y) End If End Sub End Class