Modules/EDMI_ClientSuite/EntityDesigner/frmEntityDesigner.vb
2019-01-22 16:37:35 +01:00

128 lines
4.2 KiB
VB.net

Public Class frmEntityDesigner
Private _IsMouseDown As Boolean = False
Private _IsMouseMoving As Boolean = False
Private _BeginPosition As Point = Nothing
Private _EndPosition As Point = Nothing
Private _LastCursorPosition As Point = Nothing
Private _CurrentControl As Control = Nothing
Private _ControlBuilder As ClassControlBuilder = Nothing
Private Sub frmEntityDesigner_Load(sender As Object, e As EventArgs) Handles Me.Load
' Assign Control Types to DragDrop Buttons
btnLabel.Tag = ClassControlUtils.ControlType.Label
btnTextbox.Tag = ClassControlUtils.ControlType.TextBox
_ControlBuilder = New ClassControlBuilder(DesignMode:=True)
End Sub
#Region "Control Buttons Events"
Private Sub btnControl_MouseDown(sender As Object, e As MouseEventArgs) Handles btnLabel.MouseDown, btnTextbox.MouseDown
_IsMouseDown = True
End Sub
Private Sub btnControl_MouseMove(sender As Button, e As MouseEventArgs) Handles btnLabel.MouseMove, btnTextbox.MouseMove
If _IsMouseDown Then
Dim oButton = sender
Dim oType As ClassControlUtils.ControlType = oButton.Tag
oButton.DoDragDrop(oType.ToString, DragDropEffects.Copy)
End If
End Sub
Private Sub SnapPanelMain_DragEnter(sender As Object, e As DragEventArgs) Handles PanelMain.DragEnter
If (e.Data.GetDataPresent(DataFormats.Text)) Then
e.Effect = DragDropEffects.Copy
Else
e.Effect = DragDropEffects.None
End If
End Sub
Private Sub SnapPanelMain_DragDrop(sender As Object, e As DragEventArgs) Handles PanelMain.DragDrop
Dim data As String = e.Data.GetData(DataFormats.Text)
Dim type = ClassUtils.ToEnum(Of ClassControlUtils.ControlType)(data)
HandleDragDrop(type)
End Sub
#End Region
#Region "Control Events"
Private Sub Control_MouseDown(sender As Control, e As MouseEventArgs)
If e.Button = MouseButtons.Left Then
_CurrentControl = sender
_BeginPosition = e.Location
' Set the mode flag to signal the MouseMove event handler that it
' needs to now calculate new positions for our control
_IsMouseMoving = True
End If
End Sub
Private Sub Control_MouseMove(sender As Control, e As MouseEventArgs)
If _CurrentControl Is Nothing Or Not _IsMouseMoving Then
Exit Sub
End If
Cursor = Cursors.Hand
Dim oControl = sender
Dim oCursorPosition As Point = PanelMain.PointToClient(Cursor.Position)
Dim oNewPosition As New Point(oCursorPosition.X - _BeginPosition.X, oCursorPosition.Y - _BeginPosition.Y)
' If control will be moved out the of bounds of the panel at TOP/LEFT side, exit.
If oNewPosition.X < 0 Or oNewPosition.Y < 0 Then
Exit Sub
End If
oControl.Location = oNewPosition
End Sub
Private Sub Control_MouseUp(sender As Object, e As MouseEventArgs)
If Not _IsMouseMoving Then
Exit Sub
End If
_IsMouseMoving = False
_EndPosition = e.Location
Cursor = Cursors.Default
End Sub
Private Sub SetEventHandlers(Control As Control)
AddHandler Control.MouseDown, AddressOf Control_MouseDown
AddHandler Control.MouseMove, AddressOf Control_MouseMove
AddHandler Control.MouseUp, AddressOf Control_MouseUp
End Sub
#End Region
Private Sub HandleDragDrop(type As ClassControlUtils.ControlType)
Dim oCursorPosition As Point = PanelMain.PointToClient(Cursor.Position)
Dim oControl As Control = Nothing
Select Case type
Case ClassControlUtils.ControlType.Label
oControl = _ControlBuilder.CreateLabel()
Case ClassControlUtils.ControlType.TextBox
oControl = _ControlBuilder.CreateTextbox()
Case Else
MsgBox($"Unknown Control Type {type.ToString}")
Exit Sub
End Select
' Set Location to current cursor position
oControl.Location = oCursorPosition
' Attach Eventhandlers
SetEventHandlers(oControl)
' Add the control to the panel
PanelMain.Controls.Add(oControl)
End Sub
End Class