Public Class GUI_Functions
    Private Shared angleFunctionActive As Boolean = False
    Private Shared squareFunctionActive As Boolean = False
    Private Shared squareSideFunctionActive As Boolean = False

    Private Shared pointCounter As Integer

    Public Shared pointsFunc As New Dictionary(Of String, Boolean)

    Public Shared pointsMeasurements As New Dictionary(Of String, Integer())

    Private Shared sideSquareValues As Integer() = {0, 0, 0}

    Public Shared Sub AngleButton(sender As Object, e As EventArgs)
        GUI.Controls("AngleButton").Enabled = False
        GUI.Controls("Button_Square").Enabled = False
        GUI.Controls("Button_SquareSide").Enabled = False

        Dim buttonX As Integer = GUI.Controls("AngleButton").Location.X
        Dim buttonY As Integer = GUI.Controls("AngleButton").Location.Y

        GUI.Create_TextBox("X_TextBox", "", buttonX + 20, buttonY + 40, 60)
        AddHandler GUI.Controls("X_TextBox").TextChanged, AddressOf FunctionTextCorner_Changed
        AddHandler GUI.Controls("X_TextBox").KeyPress, AddressOf FunctionText_KeyPress
        GUI.Create_LabelGUI("X_Label", "1", buttonX, buttonY + 43)

        GUI.Create_TextBox("Y_TextBox", "", buttonX + 130, buttonY + 40, 60)
        AddHandler GUI.Controls("Y_TextBox").TextChanged, AddressOf FunctionTextCorner_Changed
        AddHandler GUI.Controls("Y_TextBox").KeyPress, AddressOf FunctionText_KeyPress
        GUI.Create_LabelGUI("Y_Label", "2", buttonX + 110, buttonY + 43)

        angleFunctionActive = True
    End Sub

    Public Shared Sub SquareButtton(sender As Object, e As EventArgs)
        GUI.Controls("AngleButton").Enabled = False
        GUI.Controls("Button_Square").Enabled = False
        GUI.Controls("Button_SquareSide").Enabled = False

        Dim buttonX As Integer = GUI.Controls("Button_Square").Location.X
        Dim buttonY As Integer = GUI.Controls("Button_Square").Location.Y

        GUI.Create_TextBox("X_TextBox", "", buttonX + 20, buttonY + 40, 60)
        AddHandler GUI.Controls("X_TextBox").TextChanged, AddressOf FunctionTextCorner_Changed
        AddHandler GUI.Controls("X_TextBox").KeyPress, AddressOf FunctionText_KeyPress
        GUI.Create_LabelGUI("X_Label", "1", buttonX, buttonY + 43)

        GUI.Create_TextBox("Y_TextBox", "", buttonX + 130, buttonY + 40, 60)
        AddHandler GUI.Controls("Y_TextBox").TextChanged, AddressOf FunctionTextCorner_Changed
        AddHandler GUI.Controls("Y_TextBox").KeyPress, AddressOf FunctionText_KeyPress
        GUI.Create_LabelGUI("Y_Label", "2", buttonX + 110, buttonY + 43)

        ' Add option for corner or side

        squareFunctionActive = True
    End Sub

    Public Shared Sub SquareSideButtton(sender As Object, e As EventArgs)
        GUI.Controls("AngleButton").Enabled = False
        GUI.Controls("Button_Square").Enabled = False
        GUI.Controls("Button_SquareSide").Enabled = False

        Dim buttonX As Integer = GUI.Controls("Button_SquareSide").Location.X
        Dim buttonY As Integer = GUI.Controls("Button_SquareSide").Location.Y

        GUI.Create_TextBox("X_TextBox", "", buttonX + 20, buttonY + 40, 60)
        AddHandler GUI.Controls("X_TextBox").TextChanged, AddressOf FunctionTextSide_Changed
        AddHandler GUI.Controls("X_TextBox").KeyPress, AddressOf FunctionText_KeyPress
        GUI.Create_LabelGUI("X_Label", "L", buttonX, buttonY + 43)

        GUI.Create_TextBox("Y_TextBox", "", buttonX + 130, buttonY + 40, 60)
        AddHandler GUI.Controls("Y_TextBox").TextChanged, AddressOf FunctionTextSide_Changed
        AddHandler GUI.Controls("Y_TextBox").KeyPress, AddressOf FunctionText_KeyPress
        GUI.Create_LabelGUI("Y_Label", "W", buttonX + 110, buttonY + 43)

        GUI.Create_TextBox("Off_TextBox", "", buttonX + 250, buttonY + 40, 60)
        AddHandler GUI.Controls("Off_TextBox").TextChanged, AddressOf FunctionTextSide_Changed
        AddHandler GUI.Controls("Off_TextBox").KeyPress, AddressOf FunctionText_KeyPress
        GUI.Create_LabelGUI("Off_Label", "Offset", buttonX + 210, buttonY + 43)

        ' Add option for corner or side

        'squareSideFunctionActive = True
    End Sub

    Private Shared Sub FunctionText_KeyPress(sender As Object, e As KeyPressEventArgs)
        GUI_Gratings_Data.Check_IfNumber(e)
    End Sub

    Private Shared Sub FunctionTextCorner_Changed(sender As TextBox, e As EventArgs)
        Dim value1, value2 As Integer
        Try
            value1 = CInt(GUI.Controls("X_TextBox").Text)
            value2 = CInt(GUI.Controls("Y_TextBox").Text)
        Catch ex As Exception
            value1 = 0
            value2 = 0
        End Try
        If value1 > 0 AndAlso value2 > 0 Then
            If GUI.DrawingPanel.Controls.Count = 2 Then
                For i = 0 To GUI_Drawing_Panel.pointsOrder.Count - 1
                    If pointsFunc(GUI_Drawing_Panel.pointsOrder(i)) Then
                        Dim pointButton As New Button
                        pointButton.Width = 30
                        pointButton.Height = 30

                        pointButton.Left = GUI_Drawing_Panel.points(GUI_Drawing_Panel.pointsOrder(i))(0) - pointButton.Width / 2
                        pointButton.Top = GUI_Drawing_Panel.points(GUI_Drawing_Panel.pointsOrder(i))(1) - pointButton.Height / 2

                        pointButton.Name = GUI_Drawing_Panel.pointsOrder(i) & "_Button"
                        pointButton.Text = ""

                        pointButton.BackColor = Color.FromArgb(50, Color.Red)
                        pointButton.FlatStyle = FlatStyle.Flat
                        pointButton.FlatAppearance.BorderSize = 0
                        pointButton.FlatAppearance.MouseOverBackColor = Color.Red
                        pointButton.FlatAppearance.MouseDownBackColor = Color.DarkRed

                        Dim gp As New Drawing.Drawing2D.GraphicsPath
                        gp.AddEllipse(New Rectangle(New Point(0, 0), New Size(30, 30)))
                        pointButton.Region = New Region(gp)

                        GUI.DrawingPanel.Controls.Add(pointButton)

                        AddHandler pointButton.Click, AddressOf PointButton_Click
                    End If
                Next
                pointCounter += 1
            End If

        ElseIf GUI.DrawingPanel.Controls.Count > 2 Then
            For i = 0 To GUI_Drawing_Panel.pointsOrder.Count - 1
                If pointsFunc(GUI_Drawing_Panel.pointsOrder(i)) Then
                    RemoveHandler GUI.DrawingPanel.Controls(GUI_Drawing_Panel.pointsOrder(i) & "_Button").Click,
                    AddressOf PointButton_Click
                    GUI.DrawingPanel.Controls.RemoveByKey(GUI_Drawing_Panel.pointsOrder(i) & "_Button")
                End If
            Next

            pointCounter -= 1
        End If
    End Sub


    Private Shared Sub FunctionTextSide_Changed(sender As TextBox, e As EventArgs)
        Try
            sideSquareValues(0) = CInt(GUI.Controls("X_TextBox").Text)
            sideSquareValues(1) = CInt(GUI.Controls("Y_TextBox").Text)
            sideSquareValues(2) = CInt(GUI.Controls("Off_TextBox").Text)
        Catch ex As Exception
            sideSquareValues(0) = 0
            sideSquareValues(1) = 0
            sideSquareValues(2) = 0
        End Try

        If sideSquareValues(0) > 0 AndAlso sideSquareValues(1) > 0 AndAlso sideSquareValues(2) > 0 Then
            If GUI.DrawingPanel.Controls.Count = 2 Then
                For i = 1 To 4
                    Dim sidePoints As New List(Of String)
                    If i = 1 Then
                        Create_SideButton(sideSquareValues(0) / (GUI_Drawing_Panel.scaleDiff), 30,
                                              GUI_Drawing_Panel.pCon1(0) + sideSquareValues(2) / (GUI_Drawing_Panel.scaleDiff),
                                              GUI_Drawing_Panel.pCon1(1) - 30 / 2, "Button_" & i)
                    ElseIf i = 2 Then
                        Create_SideButton(30, sideSquareValues(1) / (GUI_Drawing_Panel.scaleDiff),
                                              GUI_Drawing_Panel.pCon2(0) - 30 / 2,
                                              GUI_Drawing_Panel.pCon1(1) + sideSquareValues(2) / (GUI_Drawing_Panel.scaleDiff), "Button_" & i)
                    ElseIf i = 3 Then
                        Create_SideButton(sideSquareValues(0) / (GUI_Drawing_Panel.scaleDiff), 30,
                                              GUI_Drawing_Panel.pCon3(0) - sideSquareValues(2) / (GUI_Drawing_Panel.scaleDiff) - sideSquareValues(0) / (GUI_Drawing_Panel.scaleDiff),
                                              GUI_Drawing_Panel.pCon3(1) - 30 / 2, "Button_" & i)
                    Else
                        Create_SideButton(30, sideSquareValues(1) / (GUI_Drawing_Panel.scaleDiff),
                                              GUI_Drawing_Panel.pCon1(0) - 30 / 2,
                                              GUI_Drawing_Panel.pCon4(1) - sideSquareValues(2) / (GUI_Drawing_Panel.scaleDiff) - sideSquareValues(1) / (GUI_Drawing_Panel.scaleDiff),
                                              "Button_" & i)
                    End If

                Next
                pointCounter = pointCounter + 1

            Else

                'flytta knapparna/ändra storlek
                For i = 2 To GUI.DrawingPanel.Controls.Count - 1
                    Dim buttonSide As Integer = CInt(GUI.DrawingPanel.Controls(i).Name.Split("_")(1))
                    If buttonSide = 1 Then
                        GUI.DrawingPanel.Controls(i).Width = sideSquareValues(0) / (GUI_Drawing_Panel.scaleDiff)
                        GUI.DrawingPanel.Controls(i).Left = GUI_Drawing_Panel.pCon1(0) + sideSquareValues(2) / (GUI_Drawing_Panel.scaleDiff)
                    ElseIf buttonSide = 2 Then
                        GUI.DrawingPanel.Controls(i).Height = sideSquareValues(1) / (GUI_Drawing_Panel.scaleDiff)
                        GUI.DrawingPanel.Controls(i).Top = GUI_Drawing_Panel.pCon1(1) + sideSquareValues(2) / (GUI_Drawing_Panel.scaleDiff)
                    ElseIf buttonSide = 3 Then
                        GUI.DrawingPanel.Controls(i).Width = sideSquareValues(0) / (GUI_Drawing_Panel.scaleDiff)
                        GUI.DrawingPanel.Controls(i).Left = GUI_Drawing_Panel.pCon3(0) - sideSquareValues(2) / (GUI_Drawing_Panel.scaleDiff) - GUI.DrawingPanel.Controls(i).Width
                    Else
                        GUI.DrawingPanel.Controls(i).Height = sideSquareValues(1) / (GUI_Drawing_Panel.scaleDiff)
                        GUI.DrawingPanel.Controls(i).Top = GUI_Drawing_Panel.pCon4(1) - sideSquareValues(2) / (GUI_Drawing_Panel.scaleDiff) - GUI.DrawingPanel.Controls(i).Height
                    End If

                Next

            End If

        ElseIf GUI.DrawingPanel.Controls.Count > 2 Then
            For i = 2 To GUI.DrawingPanel.Controls.Count - 1
                RemoveHandler GUI.DrawingPanel.Controls(2).Click, AddressOf SideButton_Click
                GUI.DrawingPanel.Controls.RemoveAt(2)
            Next

            pointCounter -= 1
        End If
    End Sub

    Private Shared Sub Create_SideButton(bWidth As Integer, bHeight As Integer, bLeft As Integer, bTop As Integer, bName As String)
        Dim sideButton As New Button

        sideButton.Width = bWidth
        sideButton.Height = bHeight

        sideButton.Left = bLeft
        sideButton.Top = bTop

        sideButton.Name = bName
        sideButton.Text = ""

        sideButton.BackColor = Color.FromArgb(50, Color.Red)
        sideButton.FlatStyle = FlatStyle.Flat
        sideButton.FlatAppearance.BorderSize = 0
        sideButton.FlatAppearance.MouseOverBackColor = Color.Red
        sideButton.FlatAppearance.MouseDownBackColor = Color.DarkRed

        GUI.DrawingPanel.Controls.Add(sideButton)

        AddHandler sideButton.Click, AddressOf SideButton_Click
    End Sub


    ' --- When a side function button is pressed ---
    Private Shared Sub SideButton_Click(sender As Button, e As EventArgs)
        'Determine which side is being pressed
        Dim sidePressed As Integer = CInt(sender.Name.Split("_")(1))

        ' Remove point buttons
        For i = 2 To GUI.DrawingPanel.Controls.Count - 1
            RemoveHandler GUI.DrawingPanel.Controls(2).Click, AddressOf SideButton_Click
            GUI.DrawingPanel.Controls.RemoveAt(2)
        Next

        Dim numOfNewPoints As Integer

        Dim sidePoints As New List(Of String)
        Dim index As Integer

        If sidePressed = 1 Then
            For j = 0 To GUI_Drawing_Panel.pointsOrder.Count - 1
                If GUI_Drawing_Panel.points(GUI_Drawing_Panel.pointsOrder(j))(1) = GUI_Drawing_Panel.pCon1(1) Then
                    sidePoints.Add(GUI_Drawing_Panel.pointsOrder(j))
                End If
            Next
            index = GUI_Drawing_Panel.pointsOrder.IndexOf(sidePoints(0))
            If sidePoints.Count > 2 Then
                For i = sidePoints.Count / 2 To 2 Step -1
                    If GUI_Drawing_Panel.points(sidePoints(i * 2 - 2))(0) <
                        GUI_Drawing_Panel.pCon1(0) + sideSquareValues(2) / (GUI_Drawing_Panel.scaleDiff) Then

                        index = GUI_Drawing_Panel.pointsOrder.IndexOf(sidePoints(i * 2 - 2))
                        Exit For
                    End If
                Next
            End If

        ElseIf sidePressed = 2 Then
            For j = 1 To GUI_Drawing_Panel.pointsOrder.Count - 1
                If GUI_Drawing_Panel.points(GUI_Drawing_Panel.pointsOrder(j))(0) = GUI_Drawing_Panel.pCon2(0) Then
                    sidePoints.Add(GUI_Drawing_Panel.pointsOrder(j))
                End If
            Next
            index = GUI_Drawing_Panel.pointsOrder.IndexOf(sidePoints(0))
            If sidePoints.Count > 2 Then
                For i = sidePoints.Count / 2 To 2 Step -1
                    If GUI_Drawing_Panel.points(sidePoints(i * 2 - 2))(1) <
                        GUI_Drawing_Panel.pCon2(1) + sideSquareValues(2) / (GUI_Drawing_Panel.scaleDiff) Then

                        index = GUI_Drawing_Panel.pointsOrder.IndexOf(sidePoints(i * 2 - 2))
                        Exit For
                    End If
                Next
            End If

        ElseIf sidePressed = 3 Then
            For j = 2 To GUI_Drawing_Panel.pointsOrder.Count - 1
                If GUI_Drawing_Panel.points(GUI_Drawing_Panel.pointsOrder(j))(1) = GUI_Drawing_Panel.pCon3(1) Then
                    sidePoints.Add(GUI_Drawing_Panel.pointsOrder(j))
                End If
            Next
            index = GUI_Drawing_Panel.pointsOrder.IndexOf(sidePoints(0))
            If sidePoints.Count > 2 Then
                For i = sidePoints.Count / 2 To 2 Step -1
                    If GUI_Drawing_Panel.points(sidePoints(i * 2 - 2))(0) >
                        GUI_Drawing_Panel.pCon3(0) - sideSquareValues(2) / (GUI_Drawing_Panel.scaleDiff) Then

                        index = GUI_Drawing_Panel.pointsOrder.IndexOf(sidePoints(i * 2 - 2))
                        Exit For
                    End If
                Next
            End If

        Else
            For j = 3 To GUI_Drawing_Panel.pointsOrder.Count - 1
                If GUI_Drawing_Panel.points(GUI_Drawing_Panel.pointsOrder(j))(0) = GUI_Drawing_Panel.pCon1(0) Then
                    sidePoints.Add(GUI_Drawing_Panel.pointsOrder(j))
                End If
            Next
            sidePoints.Add(GUI_Drawing_Panel.pointsOrder(0))

            index = GUI_Drawing_Panel.pointsOrder.IndexOf(sidePoints(0))
            If sidePoints.Count > 2 Then
                For i = sidePoints.Count / 2 To 2 Step -1
                    If GUI_Drawing_Panel.points(sidePoints(i * 2 - 2))(1) >
                        GUI_Drawing_Panel.pCon4(1) - sideSquareValues(2) / (GUI_Drawing_Panel.scaleDiff) Then

                        index = GUI_Drawing_Panel.pointsOrder.IndexOf(sidePoints(i * 2 - 2))
                        Exit For
                    End If
                Next
            End If
        End If



        For i = 2 To pointsMeasurements.Count - 1
            Dim mesName As String = pointsMeasurements.Keys(i)
            If mesName.Split("_")(0) = "SS1" Then
                If index + 2 = pointsMeasurements(mesName)(1) Then
                    If GUI_Drawing_Panel.measureLabels(mesName)(3) = 1 Then
                        GUI_Drawing_Panel.measureLabels(mesName)(0) = GUI_Drawing_Panel.points(GUI_Drawing_Panel.pointsOrder(index + 1))(2) * 1000 -
                        GUI_Drawing_Panel.pCon1(2) * 1000 - (sideSquareValues(2) + sideSquareValues(0))

                        GUI_Drawing_Panel.sideSquareMesLine(mesName)(0) = GUI_Drawing_Panel.pCon1(0) + (sideSquareValues(2) + sideSquareValues(0)) / (GUI_Drawing_Panel.scaleDiff)

                        GUI_Drawing_Panel.measureLabels(mesName)(1) = (GUI_Drawing_Panel.sideSquareMesLine(mesName)(0) + GUI_Drawing_Panel.sideSquareMesLine(mesName)(2)) / 2 - 8

                    ElseIf GUI_Drawing_Panel.measureLabels(mesName)(3) = 2 Then
                        GUI_Drawing_Panel.measureLabels(mesName)(0) = GUI_Drawing_Panel.pCon2(3) * 1000 - GUI_Drawing_Panel.points(GUI_Drawing_Panel.pointsOrder(index + 1))(3) * 1000 -
                         (sideSquareValues(2) + sideSquareValues(1))

                        GUI_Drawing_Panel.sideSquareMesLine(mesName)(1) = GUI_Drawing_Panel.pCon2(1) + (sideSquareValues(2) + sideSquareValues(1)) / (GUI_Drawing_Panel.scaleDiff)

                        GUI_Drawing_Panel.measureLabels(mesName)(2) = (GUI_Drawing_Panel.sideSquareMesLine(mesName)(1) + GUI_Drawing_Panel.sideSquareMesLine(mesName)(3)) / 2 - 6

                    ElseIf GUI_Drawing_Panel.measureLabels(mesName)(3) = 3 Then
                        GUI_Drawing_Panel.measureLabels(mesName)(0) = GUI_Drawing_Panel.pCon3(2) * 1000 - GUI_Drawing_Panel.points(GUI_Drawing_Panel.pointsOrder(index + 1))(2) * 1000 _
                         - (sideSquareValues(2) + sideSquareValues(0))

                        GUI_Drawing_Panel.sideSquareMesLine(mesName)(0) = GUI_Drawing_Panel.pCon3(0) - (sideSquareValues(2) + sideSquareValues(0)) / (GUI_Drawing_Panel.scaleDiff)

                        GUI_Drawing_Panel.measureLabels(mesName)(1) = (GUI_Drawing_Panel.sideSquareMesLine(mesName)(0) + GUI_Drawing_Panel.sideSquareMesLine(mesName)(2)) / 2 - 8

                    Else
                        GUI_Drawing_Panel.measureLabels(mesName)(0) = GUI_Drawing_Panel.points(GUI_Drawing_Panel.pointsOrder(index + 1))(3) * 1000 -
                         GUI_Drawing_Panel.pCon4(3) * 1000 - (sideSquareValues(2) + sideSquareValues(1))

                        GUI_Drawing_Panel.sideSquareMesLine(mesName)(1) = GUI_Drawing_Panel.pCon4(1) - (sideSquareValues(2) + sideSquareValues(1)) / (GUI_Drawing_Panel.scaleDiff)

                        GUI_Drawing_Panel.measureLabels(mesName)(2) = (GUI_Drawing_Panel.sideSquareMesLine(mesName)(1) + GUI_Drawing_Panel.sideSquareMesLine(mesName)(3)) / 2 - 6

                    End If
                End If
            End If
        Next



        For i = 2 To pointsMeasurements.Count - 1
            Dim mesName As String = pointsMeasurements.Keys(i)
            If mesName.Split("_")(0) <> "SS1" AndAlso index + 1 < pointsMeasurements(mesName)(0) Then
                pointsMeasurements(mesName)(0) = pointsMeasurements(mesName)(0) + 4
                pointsMeasurements(mesName)(1) = pointsMeasurements(mesName)(1) + 4
            ElseIf mesName.Split("_")(0) = "SS1" AndAlso index + 2 <= pointsMeasurements(mesName)(1) Then
                pointsMeasurements(mesName)(1) = pointsMeasurements(mesName)(1) + 4
            End If
        Next

        Dim mesNum As Integer = pointsMeasurements.Count - 1
        If sidePressed = 1 Then
            GUI_Drawing_Panel.points.Add("pSS" & pointCounter * 4 - 3,
                                         {GUI_Drawing_Panel.pCon1(0) + sideSquareValues(2) / (GUI_Drawing_Panel.scaleDiff),
                                         GUI_Drawing_Panel.pCon1(1),
                                         GUI_Drawing_Panel.pCon1(2) + sideSquareValues(2) / 1000,
                                         GUI_Drawing_Panel.pCon1(3)})
            GUI_Drawing_Panel.points.Add("pSS" & pointCounter * 4 - 2,
                                         {GUI_Drawing_Panel.pCon1(0) + sideSquareValues(2) / (GUI_Drawing_Panel.scaleDiff),
                                         GUI_Drawing_Panel.pCon1(1) + sideSquareValues(1) / (GUI_Drawing_Panel.scaleDiff),
                                         GUI_Drawing_Panel.pCon1(2) + sideSquareValues(2) / 1000,
                                         GUI_Drawing_Panel.pCon1(3) - sideSquareValues(1) / 1000})
            GUI_Drawing_Panel.points.Add("pSS" & pointCounter * 4 - 1,
                                         {GUI_Drawing_Panel.pCon1(0) + (sideSquareValues(2) + sideSquareValues(0)) / (GUI_Drawing_Panel.scaleDiff),
                                         GUI_Drawing_Panel.pCon1(1) + sideSquareValues(1) / (GUI_Drawing_Panel.scaleDiff),
                                         GUI_Drawing_Panel.pCon1(2) + (sideSquareValues(2) + sideSquareValues(0)) / 1000,
                                         GUI_Drawing_Panel.pCon1(3) - sideSquareValues(1) / 1000})
            GUI_Drawing_Panel.points.Add("pSS" & pointCounter * 4,
                                        {GUI_Drawing_Panel.pCon1(0) + (sideSquareValues(2) + sideSquareValues(0)) / (GUI_Drawing_Panel.scaleDiff),
                                        GUI_Drawing_Panel.pCon1(1),
                                        GUI_Drawing_Panel.pCon1(2) + (sideSquareValues(2) + sideSquareValues(0)) / 1000,
                                        GUI_Drawing_Panel.pCon1(3)})
            'Outer measuements
            pointsMeasurements("Lmes")(1) = pointsMeasurements("Lmes")(1) + 4
            pointsMeasurements("Wmes")(0) = pointsMeasurements("Wmes")(0) + 4
            pointsMeasurements("Wmes")(1) = pointsMeasurements("Wmes")(1) + 4

            'SideSquare measurements
            pointsMeasurements.Add("SS1_" & mesNum, {GUI_Drawing_Panel.pointsOrder.IndexOf(sidePoints(0)) + 1, index + 2, 1})
            pointsMeasurements.Add("SS_" & mesNum + 1, {index + 2, index + 5, 1})
            pointsMeasurements.Add("SS_" & mesNum + 2, {index + 2, index + 3, 4})

            'Support Line for sidesquare measurement
            GUI_Drawing_Panel.sideSquareMesLine.Add("SS1_" & mesNum, {GUI_Drawing_Panel.points(GUI_Drawing_Panel.pointsOrder(index))(0),
                                                     GUI_Drawing_Panel.pCon1(1) - 15,
                                                    GUI_Drawing_Panel.pCon1(0) + sideSquareValues(2) / (GUI_Drawing_Panel.scaleDiff),
                                                    GUI_Drawing_Panel.pCon1(1) - 15})


            'Label
            GUI_Drawing_Panel.measureLabels.Add("SS1_" & mesNum, {CInt(GUI.Controls("Off_TextBox").Text) - (GUI_Drawing_Panel.points(GUI_Drawing_Panel.pointsOrder(index))(2) * 1000 + GUI_Gratings_Data.gratingMaxL / 2),
                                                (GUI_Drawing_Panel.points(GUI_Drawing_Panel.pointsOrder(index))(0) + GUI_Drawing_Panel.pCon1(0) + sideSquareValues(2) / (GUI_Drawing_Panel.scaleDiff)) / 2 - 8,
                                                GUI_Drawing_Panel.pCon1(1) - 15 - 12 - 3,
                                                1}) '15 är linjen, 12 är textens höjd, 3 är lite extra
            GUI_Drawing_Panel.measureLabels.Add("SS_" & mesNum + 1, {CInt(GUI.Controls("X_TextBox").Text),
                                                GUI_Drawing_Panel.pCon1(0) + sideSquareValues(2) / (GUI_Drawing_Panel.scaleDiff) + CInt(GUI.Controls("X_TextBox").Text) / (2 * GUI_Drawing_Panel.scaleDiff) - 8,
                                                GUI_Drawing_Panel.pCon1(1) + sideSquareValues(1) / (GUI_Drawing_Panel.scaleDiff) + 3,
                                                1})
            GUI_Drawing_Panel.measureLabels.Add("SS_" & mesNum + 2, {CInt(GUI.Controls("Y_TextBox").Text),
                                                GUI_Drawing_Panel.pCon1(0) + sideSquareValues(2) / (GUI_Drawing_Panel.scaleDiff) - 18 - 3,
                                                GUI_Drawing_Panel.pCon1(1) + CInt(GUI.Controls("Y_TextBox").Text) / (2 * GUI_Drawing_Panel.scaleDiff) - 4,
                                                1})


        ElseIf sidePressed = 2 Then
            GUI_Drawing_Panel.points.Add("pSS" & pointCounter * 4 - 3,
                                         {GUI_Drawing_Panel.pCon2(0),
                                         GUI_Drawing_Panel.pCon2(1) + sideSquareValues(2) / (GUI_Drawing_Panel.scaleDiff),
                                         GUI_Drawing_Panel.pCon2(2),
                                         GUI_Drawing_Panel.pCon2(3) - sideSquareValues(2) / 1000})
            GUI_Drawing_Panel.points.Add("pSS" & pointCounter * 4 - 2,
                                         {GUI_Drawing_Panel.pCon2(0) - sideSquareValues(0) / (GUI_Drawing_Panel.scaleDiff),
                                         GUI_Drawing_Panel.pCon2(1) + sideSquareValues(2) / (GUI_Drawing_Panel.scaleDiff),
                                         GUI_Drawing_Panel.pCon2(2) - sideSquareValues(0) / 1000,
                                         GUI_Drawing_Panel.pCon2(3) - sideSquareValues(2) / 1000})
            GUI_Drawing_Panel.points.Add("pSS" & pointCounter * 4 - 1,
                                         {GUI_Drawing_Panel.pCon2(0) - sideSquareValues(0) / (GUI_Drawing_Panel.scaleDiff),
                                         GUI_Drawing_Panel.pCon2(1) + (sideSquareValues(2) + sideSquareValues(1)) / (GUI_Drawing_Panel.scaleDiff),
                                         GUI_Drawing_Panel.pCon2(2) - sideSquareValues(0) / 1000,
                                         GUI_Drawing_Panel.pCon2(3) - (sideSquareValues(2) + sideSquareValues(1)) / 1000})
            GUI_Drawing_Panel.points.Add("pSS" & pointCounter * 4,
                                        {GUI_Drawing_Panel.pCon2(0),
                                        GUI_Drawing_Panel.pCon2(1) + (sideSquareValues(2) + sideSquareValues(1)) / (GUI_Drawing_Panel.scaleDiff),
                                        GUI_Drawing_Panel.pCon2(2),
                                       GUI_Drawing_Panel.pCon2(3) - (sideSquareValues(2) + sideSquareValues(1)) / 1000})
            'Outer measuements
            pointsMeasurements("Wmes")(1) = pointsMeasurements("Wmes")(1) + 4

            'SideSquare measurements
            pointsMeasurements.Add("SS1_" & mesNum, {GUI_Drawing_Panel.pointsOrder.IndexOf(sidePoints(0)) + 1, index + 2, 2})
            pointsMeasurements.Add("SS_" & mesNum + 1, {index + 2, index + 5, 2})
            pointsMeasurements.Add("SS_" & mesNum + 2, {index + 2, index + 3, 3})

            'Support Line for sidesquare measurement
            GUI_Drawing_Panel.sideSquareMesLine.Add("SS1_" & mesNum, {GUI_Drawing_Panel.pCon2(0) + 15,
                                                    GUI_Drawing_Panel.points(GUI_Drawing_Panel.pointsOrder(index))(1),
                                                    GUI_Drawing_Panel.pCon2(0) + 15,
                                                    GUI_Drawing_Panel.pCon2(1) + sideSquareValues(2) / (GUI_Drawing_Panel.scaleDiff)})

            'Label
            GUI_Drawing_Panel.measureLabels.Add("SS1_" & mesNum, {CInt(GUI.Controls("Off_TextBox").Text) - (GUI_Gratings_Data.gratingMaxW / 2 - GUI_Drawing_Panel.points(GUI_Drawing_Panel.pointsOrder(index))(3) * 1000),
                                                GUI_Drawing_Panel.pCon2(0) + 15 + 3,
                                                (GUI_Drawing_Panel.points(GUI_Drawing_Panel.pointsOrder(index))(1) + GUI_Drawing_Panel.pCon2(1) + sideSquareValues(2) / (GUI_Drawing_Panel.scaleDiff)) / 2 - 4,
                                                2})

            GUI_Drawing_Panel.measureLabels.Add("SS_" & mesNum + 1, {CInt(GUI.Controls("Y_TextBox").Text),
                                                GUI_Drawing_Panel.pCon2(0) - sideSquareValues(0) / (GUI_Drawing_Panel.scaleDiff) - 18 - 3,
                                                GUI_Drawing_Panel.pCon2(1) + (sideSquareValues(2) + sideSquareValues(1) / 2) / GUI_Drawing_Panel.scaleDiff - 4,
                                                2})

            GUI_Drawing_Panel.measureLabels.Add("SS_" & mesNum + 2, {CInt(GUI.Controls("X_TextBox").Text),
                                                GUI_Drawing_Panel.pCon2(0) - sideSquareValues(0) / (2 * GUI_Drawing_Panel.scaleDiff) - 8,
                                                GUI_Drawing_Panel.pCon2(1) + sideSquareValues(2) / (GUI_Drawing_Panel.scaleDiff) - 15 - 3,
                                                2})

        ElseIf sidePressed = 3 Then
            GUI_Drawing_Panel.points.Add("pSS" & pointCounter * 4 - 3,
                                         {GUI_Drawing_Panel.pCon3(0) - sideSquareValues(2) / (GUI_Drawing_Panel.scaleDiff),
                                         GUI_Drawing_Panel.pCon3(1),
                                         GUI_Drawing_Panel.pCon3(2) - sideSquareValues(2) / 1000,
                                         GUI_Drawing_Panel.pCon3(3)})
            GUI_Drawing_Panel.points.Add("pSS" & pointCounter * 4 - 2,
                                         {GUI_Drawing_Panel.pCon3(0) - sideSquareValues(2) / (GUI_Drawing_Panel.scaleDiff),
                                         GUI_Drawing_Panel.pCon3(1) - sideSquareValues(1) / (GUI_Drawing_Panel.scaleDiff),
                                         GUI_Drawing_Panel.pCon3(2) - sideSquareValues(2) / 1000,
                                         GUI_Drawing_Panel.pCon3(3) + sideSquareValues(1) / 1000})
            GUI_Drawing_Panel.points.Add("pSS" & pointCounter * 4 - 1,
                                         {GUI_Drawing_Panel.pCon3(0) - (sideSquareValues(2) + sideSquareValues(0)) / (GUI_Drawing_Panel.scaleDiff),
                                         GUI_Drawing_Panel.pCon3(1) - sideSquareValues(1) / (GUI_Drawing_Panel.scaleDiff),
                                         GUI_Drawing_Panel.pCon3(2) - (sideSquareValues(2) + sideSquareValues(0)) / 1000,
                                         GUI_Drawing_Panel.pCon3(3) + sideSquareValues(1) / 1000})
            GUI_Drawing_Panel.points.Add("pSS" & pointCounter * 4,
                                         {GUI_Drawing_Panel.pCon3(0) - (sideSquareValues(2) + sideSquareValues(0)) / (GUI_Drawing_Panel.scaleDiff),
                                         GUI_Drawing_Panel.pCon3(1),
                                         GUI_Drawing_Panel.pCon3(2) - (sideSquareValues(2) + sideSquareValues(0)) / 1000,
                                         GUI_Drawing_Panel.pCon3(3)})

            pointsMeasurements.Add("SS1_" & mesNum, {GUI_Drawing_Panel.pointsOrder.IndexOf(sidePoints(0)) + 1, index + 2, 3})
            pointsMeasurements.Add("SS_" & mesNum + 1, {index + 2, index + 5, 3})
            pointsMeasurements.Add("SS_" & mesNum + 2, {index + 2, index + 3, 4})

            'Support Line for sidesquare measurement
            GUI_Drawing_Panel.sideSquareMesLine.Add("SS1_" & mesNum, {GUI_Drawing_Panel.points(GUI_Drawing_Panel.pointsOrder(index))(0),
                                                     GUI_Drawing_Panel.pCon3(1) + 15,
                                                    GUI_Drawing_Panel.pCon3(0) - sideSquareValues(2) / (GUI_Drawing_Panel.scaleDiff),
                                                    GUI_Drawing_Panel.pCon3(1) + 15})

            'Label
            GUI_Drawing_Panel.measureLabels.Add("SS1_" & mesNum, {CInt(GUI.Controls("Off_TextBox").Text) - (GUI_Gratings_Data.gratingMaxL / 2 - GUI_Drawing_Panel.points(GUI_Drawing_Panel.pointsOrder(index))(2) * 1000),
                                                (GUI_Drawing_Panel.points(GUI_Drawing_Panel.pointsOrder(index))(0) + GUI_Drawing_Panel.pCon3(0) - sideSquareValues(2) / (GUI_Drawing_Panel.scaleDiff)) / 2 - 8,
                                                GUI_Drawing_Panel.pCon3(1) + 15 + 3,
                                                3}) '15 är linjen, 12 är textens höjd, 3 är lite extra
            GUI_Drawing_Panel.measureLabels.Add("SS_" & mesNum + 1, {CInt(GUI.Controls("X_TextBox").Text),
                                                GUI_Drawing_Panel.pCon3(0) - sideSquareValues(2) / (GUI_Drawing_Panel.scaleDiff) - CInt(GUI.Controls("X_TextBox").Text) / (2 * GUI_Drawing_Panel.scaleDiff) - 8,
                                                GUI_Drawing_Panel.pCon3(1) - sideSquareValues(1) / (GUI_Drawing_Panel.scaleDiff) - 12 - 3,
                                                3})
            GUI_Drawing_Panel.measureLabels.Add("SS_" & mesNum + 2, {CInt(GUI.Controls("Y_TextBox").Text),
                                                GUI_Drawing_Panel.pCon3(0) - sideSquareValues(2) / (GUI_Drawing_Panel.scaleDiff) + 3,
                                                GUI_Drawing_Panel.pCon3(1) - CInt(GUI.Controls("Y_TextBox").Text) / (2 * GUI_Drawing_Panel.scaleDiff) - 4,
                                                3})

        Else
            GUI_Drawing_Panel.points.Add("pSS" & pointCounter * 4 - 3,
                                        {GUI_Drawing_Panel.pCon4(0),
                                        GUI_Drawing_Panel.pCon4(1) - sideSquareValues(2) / (GUI_Drawing_Panel.scaleDiff),
                                        GUI_Drawing_Panel.pCon4(2),
                                        GUI_Drawing_Panel.pCon4(3) + sideSquareValues(2) / 1000})
            GUI_Drawing_Panel.points.Add("pSS" & pointCounter * 4 - 2,
                                         {GUI_Drawing_Panel.pCon4(0) + sideSquareValues(0) / (GUI_Drawing_Panel.scaleDiff),
                                         GUI_Drawing_Panel.pCon4(1) - sideSquareValues(2) / (GUI_Drawing_Panel.scaleDiff),
                                         GUI_Drawing_Panel.pCon4(2) + sideSquareValues(0) / 1000,
                                         GUI_Drawing_Panel.pCon4(3) + sideSquareValues(2) / 1000})
            GUI_Drawing_Panel.points.Add("pSS" & pointCounter * 4 - 1,
                                         {GUI_Drawing_Panel.pCon4(0) + sideSquareValues(0) / (GUI_Drawing_Panel.scaleDiff),
                                         GUI_Drawing_Panel.pCon4(1) - (sideSquareValues(2) + sideSquareValues(1)) / (GUI_Drawing_Panel.scaleDiff),
                                         GUI_Drawing_Panel.pCon4(2) + sideSquareValues(0) / 1000,
                                         GUI_Drawing_Panel.pCon4(3) + (sideSquareValues(2) + sideSquareValues(1)) / 1000})
            GUI_Drawing_Panel.points.Add("pSS" & pointCounter * 4,
                                        {GUI_Drawing_Panel.pCon4(0),
                                        GUI_Drawing_Panel.pCon4(1) - (sideSquareValues(2) + sideSquareValues(1)) / (GUI_Drawing_Panel.scaleDiff),
                                        GUI_Drawing_Panel.pCon4(2),
                                        GUI_Drawing_Panel.pCon4(3) + (sideSquareValues(2) + sideSquareValues(1)) / 1000})

            pointsMeasurements.Add("SS1_" & mesNum, {GUI_Drawing_Panel.pointsOrder.IndexOf(sidePoints(0)) + 1, index + 2, 4})
            pointsMeasurements.Add("SS_" & mesNum + 1, {index + 2, index + 5, 4})
            pointsMeasurements.Add("SS_" & mesNum + 2, {index + 2, index + 3, 3})

            'Support Line for sidesquare measurement
            GUI_Drawing_Panel.sideSquareMesLine.Add("SS1_" & mesNum, {GUI_Drawing_Panel.pCon4(0) - 15,
                                                    GUI_Drawing_Panel.points(GUI_Drawing_Panel.pointsOrder(index))(1),
                                                    GUI_Drawing_Panel.pCon4(0) - 15,
                                                    GUI_Drawing_Panel.pCon4(1) - sideSquareValues(2) / (GUI_Drawing_Panel.scaleDiff)})

            'Label
            GUI_Drawing_Panel.measureLabels.Add("SS1_" & mesNum, {CInt(GUI.Controls("Off_TextBox").Text) - (GUI_Gratings_Data.gratingMaxW / 2 + GUI_Drawing_Panel.points(GUI_Drawing_Panel.pointsOrder(index))(3) * 1000),
                                                GUI_Drawing_Panel.pCon4(0) - 15 - 18 - 3,
                                                (GUI_Drawing_Panel.points(GUI_Drawing_Panel.pointsOrder(index))(1) + GUI_Drawing_Panel.pCon4(1) - sideSquareValues(2) / (GUI_Drawing_Panel.scaleDiff)) / 2 - 4,
                                                4})

            GUI_Drawing_Panel.measureLabels.Add("SS_" & mesNum + 1, {CInt(GUI.Controls("Y_TextBox").Text),
                                                GUI_Drawing_Panel.pCon4(0) + sideSquareValues(0) / (GUI_Drawing_Panel.scaleDiff) + 3,
                                                GUI_Drawing_Panel.pCon4(1) - (sideSquareValues(2) + sideSquareValues(1) / 2) / GUI_Drawing_Panel.scaleDiff - 4,
                                                4})

            GUI_Drawing_Panel.measureLabels.Add("SS_" & mesNum + 2, {CInt(GUI.Controls("X_TextBox").Text),
                                                GUI_Drawing_Panel.pCon4(0) + sideSquareValues(0) / (2 * GUI_Drawing_Panel.scaleDiff) - 8,
                                                GUI_Drawing_Panel.pCon4(1) - sideSquareValues(2) / (GUI_Drawing_Panel.scaleDiff) + 3,
                                                4})

        End If

        pointsFunc.Add("pSS" & pointCounter * 4 - 3, False)
        pointsFunc.Add("pSS" & pointCounter * 4 - 2, False)
        pointsFunc.Add("pSS" & pointCounter * 4 - 1, False)
        pointsFunc.Add("pSS" & pointCounter * 4, False)

        'Insert four new points 
        GUI_Drawing_Panel.pointsOrder.Insert(index + 1, "pSS" & pointCounter * 4 - 3)
        GUI_Drawing_Panel.pointsOrder.Insert(index + 2, "pSS" & pointCounter * 4 - 2)
        GUI_Drawing_Panel.pointsOrder.Insert(index + 3, "pSS" & pointCounter * 4 - 1)
        GUI_Drawing_Panel.pointsOrder.Insert(index + 4, "pSS" & pointCounter * 4)

        numOfNewPoints = 4

        'Redraw grating
        GUI.DrawingPanel.Refresh()

        RemoveHandler GUI.Controls("X_TextBox").TextChanged, AddressOf FunctionTextSide_Changed
        RemoveHandler GUI.Controls("X_TextBox").KeyPress, AddressOf FunctionText_KeyPress
        GUI.Controls.RemoveByKey("X_TextBox")

        RemoveHandler GUI.Controls("Y_TextBox").TextChanged, AddressOf FunctionTextSide_Changed
        RemoveHandler GUI.Controls("Y_TextBox").KeyPress, AddressOf FunctionText_KeyPress
        GUI.Controls.RemoveByKey("Y_TextBox")

        RemoveHandler GUI.Controls("Off_TextBox").TextChanged, AddressOf FunctionTextSide_Changed
        RemoveHandler GUI.Controls("Off_TextBox").KeyPress, AddressOf FunctionText_KeyPress
        GUI.Controls.RemoveByKey("Off_TextBox")

        GUI.Controls.RemoveByKey("X_Label")
        GUI.Controls.RemoveByKey("Y_Label")
        GUI.Controls.RemoveByKey("Off_Label")

        GUI.Controls("AngleButton").Enabled = True
        GUI.Controls("Button_Square").Enabled = True
        GUI.Controls("Button_SquareSide").Enabled = True




    End Sub

    ' --- When a corner function button is pressed ---
    Private Shared Sub PointButton_Click(sender As Button, e As EventArgs)
        'Determine which point is being pressed
        Dim pointPressed As String = sender.Name.Split("_")(0)
        Dim index As Integer = GUI_Drawing_Panel.pointsOrder.IndexOf(pointPressed)

        ' Remove point buttons
        For i = 0 To GUI_Drawing_Panel.pointsOrder.Count - 1
            If pointsFunc(GUI_Drawing_Panel.pointsOrder(i)) Then
                RemoveHandler GUI.DrawingPanel.Controls(GUI_Drawing_Panel.pointsOrder(i) & "_Button").Click,
                    AddressOf PointButton_Click
                GUI.DrawingPanel.Controls.RemoveByKey(GUI_Drawing_Panel.pointsOrder(i) & "_Button")
            End If
        Next

        'Retrive that points coords
        Dim pXP As Decimal = GUI_Drawing_Panel.points(pointPressed)(0)
        Dim pYP As Decimal = GUI_Drawing_Panel.points(pointPressed)(1)
        Dim pXSW As Decimal = GUI_Drawing_Panel.points(pointPressed)(2)
        Dim pYSW As Decimal = GUI_Drawing_Panel.points(pointPressed)(3)

        Dim d1P As Decimal = GUI.Controls("X_TextBox").Text / GUI_Drawing_Panel.scaleDiff   ' BEHÖVS Cdec??
        Dim d2P As Decimal = GUI.Controls("Y_TextBox").Text / GUI_Drawing_Panel.scaleDiff
        Dim d1SW As Decimal = GUI.Controls("X_TextBox").Text / 1000
        Dim d2SW As Decimal = GUI.Controls("Y_TextBox").Text / 1000

        Dim numOfNewPoints As Integer


        For i = 2 To pointsMeasurements.Count - 1
            Dim mesName As String = pointsMeasurements.Keys(i)
            If mesName.Split("_")(0) <> "SS1" AndAlso mesName.Split("_")(0) <> "SS" Then
                If index + 1 = pointsMeasurements(mesName)(0) Then
                    If GUI_Drawing_Panel.measureLabels(mesName)(3) = 1 Then
                        GUI_Drawing_Panel.measureLabels(mesName)(0) = GUI_Drawing_Panel.measureLabels(mesName)(0) - GUI.Controls("X_TextBox").Text
                        GUI_Drawing_Panel.measureLabels(mesName)(1) = GUI_Drawing_Panel.measureLabels(mesName)(1) + d1P / 2
                    ElseIf GUI_Drawing_Panel.measureLabels(mesName)(3) = 2 Then
                        GUI_Drawing_Panel.measureLabels(mesName)(0) = GUI_Drawing_Panel.measureLabels(mesName)(0) - GUI.Controls("Y_TextBox").Text
                        GUI_Drawing_Panel.measureLabels(mesName)(2) = GUI_Drawing_Panel.measureLabels(mesName)(2) + d2P / 2
                    ElseIf GUI_Drawing_Panel.measureLabels(mesName)(3) = 3 Then
                        GUI_Drawing_Panel.measureLabels(mesName)(0) = GUI_Drawing_Panel.measureLabels(mesName)(0) - GUI.Controls("X_TextBox").Text
                        GUI_Drawing_Panel.measureLabels(mesName)(1) = GUI_Drawing_Panel.measureLabels(mesName)(1) - d1P / 2
                    Else
                        GUI_Drawing_Panel.measureLabels(mesName)(0) = GUI_Drawing_Panel.measureLabels(mesName)(0) - GUI.Controls("Y_TextBox").Text
                        GUI_Drawing_Panel.measureLabels(mesName)(2) = GUI_Drawing_Panel.measureLabels(mesName)(2) - d2P / 2
                    End If
                ElseIf index + 1 = pointsMeasurements(mesName)(1) Then
                    If GUI_Drawing_Panel.measureLabels(mesName)(3) = 1 Then
                        GUI_Drawing_Panel.measureLabels(mesName)(0) = GUI_Drawing_Panel.measureLabels(mesName)(0) - GUI.Controls("Y_TextBox").Text
                        GUI_Drawing_Panel.measureLabels(mesName)(2) = GUI_Drawing_Panel.measureLabels(mesName)(2) + d2P / 2
                    ElseIf GUI_Drawing_Panel.measureLabels(mesName)(3) = 2 Then
                        GUI_Drawing_Panel.measureLabels(mesName)(0) = GUI_Drawing_Panel.measureLabels(mesName)(0) - GUI.Controls("X_TextBox").Text
                        GUI_Drawing_Panel.measureLabels(mesName)(1) = GUI_Drawing_Panel.measureLabels(mesName)(1) - d1P / 2
                    ElseIf GUI_Drawing_Panel.measureLabels(mesName)(3) = 3 Then
                        GUI_Drawing_Panel.measureLabels(mesName)(0) = GUI_Drawing_Panel.measureLabels(mesName)(0) - GUI.Controls("Y_TextBox").Text
                        GUI_Drawing_Panel.measureLabels(mesName)(2) = GUI_Drawing_Panel.measureLabels(mesName)(2) - d2P / 2
                    Else
                        GUI_Drawing_Panel.measureLabels(mesName)(0) = GUI_Drawing_Panel.measureLabels(mesName)(0) - GUI.Controls("X_TextBox").Text
                        GUI_Drawing_Panel.measureLabels(mesName)(1) = GUI_Drawing_Panel.measureLabels(mesName)(1) + d1P / 2
                    End If
                End If

            ElseIf mesName.Split("_")(0) = "SS1" AndAlso index + 2 = pointsMeasurements(mesName)(1) Then
                If GUI_Drawing_Panel.measureLabels(mesName)(3) = 1 Then
                    GUI_Drawing_Panel.measureLabels(mesName)(0) = GUI_Drawing_Panel.measureLabels(mesName)(0) - GUI.Controls("X_TextBox").Text
                    GUI_Drawing_Panel.measureLabels(mesName)(1) = GUI_Drawing_Panel.measureLabels(mesName)(1) + d1P / 2

                    GUI_Drawing_Panel.sideSquareMesLine(mesName)(0) = GUI_Drawing_Panel.sideSquareMesLine(mesName)(0) + d1P
                End If
            End If
        Next

        Dim mesNum As Integer = pointsMeasurements.Count - 1
        If angleFunctionActive Then
            For i = 2 To pointsMeasurements.Count - 1
                Dim mesName As String = pointsMeasurements.Keys(i)
                If index + 1 < pointsMeasurements(mesName)(0) Then
                    pointsMeasurements(mesName)(0) = pointsMeasurements(mesName)(0) + 1
                    pointsMeasurements(mesName)(1) = pointsMeasurements(mesName)(1) + 1
                ElseIf index + 1 = pointsMeasurements(mesName)(0) OrElse index + 1 = pointsMeasurements(mesName)(1) Then
                    pointsMeasurements(mesName)(1) = pointsMeasurements(mesName)(1) + 1
                End If
            Next

            If pXP < GUI_Drawing_Panel.containerMidX Then
                If pYP > GUI_Drawing_Panel.containerMidY Then
                    'Kvadrant 4
                    GUI_Drawing_Panel.points.Add("pA" & pointCounter * 2 - 1, {pXP + d1P, pYP, pXSW + d1SW, pYSW})
                    GUI_Drawing_Panel.points.Add("pA" & pointCounter * 2, {pXP, pYP - d2P, pXSW, pYSW + d2SW})

                    'Recess measuements
                    pointsMeasurements.Add("A_" & mesNum, {index + 1, index + 2, 3})
                    pointsMeasurements.Add("A_" & mesNum + 1, {index + 1, index + 2, 4})

                    'Panel recess measurements
                    GUI_Drawing_Panel.anglePoints.Add("A" & pointCounter * 2 - 1, {pXP + d1P, pYP - d2P})
                    GUI_Drawing_Panel.anglePointsComp.Add("A" & pointCounter * 2 - 1, {"pA" & pointCounter * 2 - 1, "pA" & pointCounter * 2})

                    'Label
                    GUI_Drawing_Panel.measureLabels.Add("A_" & mesNum, {CInt(GUI.Controls("Y_TextBox").Text), pXP + d1P + 3,
                                                        pYP - d2P / 2 - 4, 4})
                    GUI_Drawing_Panel.measureLabels.Add("A_" & mesNum + 1, {CInt(GUI.Controls("X_TextBox").Text), pXP + d1P / 2 - 8,
                                                        pYP - d2P - 12 - 3, 4})
                Else
                    'Kvadrant 1
                    GUI_Drawing_Panel.points.Add("pA" & pointCounter * 2 - 1, {pXP, pYP + d2P, pXSW, pYSW - d2SW})
                    GUI_Drawing_Panel.points.Add("pA" & pointCounter * 2, {pXP + d1P, pYP, pXSW + d1SW, pYSW})

                    'Outer measuements
                    pointsMeasurements("Lmes")(1) = pointsMeasurements("Lmes")(1) + 1
                    pointsMeasurements("Wmes")(0) = pointsMeasurements("Wmes")(0) + 1
                    pointsMeasurements("Wmes")(1) = pointsMeasurements("Wmes")(1) + 1

                    'SW Recess measuements
                    pointsMeasurements.Add("A_" & mesNum, {index + 1, index + 2, 1})
                    pointsMeasurements.Add("A_" & mesNum + 1, {index + 1, index + 2, 4})

                    'Panel recess measurements
                    GUI_Drawing_Panel.anglePoints.Add("A" & pointCounter * 2 - 1, {pXP + d1P, pYP + d2P})
                    GUI_Drawing_Panel.anglePointsComp.Add("A" & pointCounter * 2 - 1, {"pA" & pointCounter * 2 - 1, "pA" & pointCounter * 2})

                    'Label
                    GUI_Drawing_Panel.measureLabels.Add("A_" & mesNum, {CInt(GUI.Controls("X_TextBox").Text), pXP + d1P / 2 - 8,
                                                        pYP + d2P + 3, 1})
                    GUI_Drawing_Panel.measureLabels.Add("A_" & mesNum + 1, {CInt(GUI.Controls("Y_TextBox").Text), pXP + d1P + 3,
                                                        pYP + d2P / 2 - 4, 1})
                End If
            Else
                If pYP > GUI_Drawing_Panel.containerMidY Then
                    'Kvadrant 3
                    GUI_Drawing_Panel.points.Add("pA" & pointCounter * 2 - 1, {pXP, pYP - d2P, pXSW, pYSW + d2SW})
                    GUI_Drawing_Panel.points.Add("pA" & pointCounter * 2, {pXP - d1P, pYP, pXSW - d1SW, pYSW})

                    pointsMeasurements("Wmes")(1) = pointsMeasurements("Wmes")(1) + 1

                    'Recess measuements
                    pointsMeasurements.Add("A_" & mesNum, {index + 1, index + 2, 2})
                    pointsMeasurements.Add("A_" & mesNum + 1, {index + 1, index + 2, 3})

                    'Panel recess measurements
                    GUI_Drawing_Panel.anglePoints.Add("A" & pointCounter * 2 - 1, {pXP - d1P, pYP - d2P})
                    GUI_Drawing_Panel.anglePointsComp.Add("A" & pointCounter * 2 - 1, {"pA" & pointCounter * 2 - 1, "pA" & pointCounter * 2})

                    'Label
                    GUI_Drawing_Panel.measureLabels.Add("A_" & mesNum, {CInt(GUI.Controls("X_TextBox").Text), pXP - d1P / 2 - 8,
                                                        pYP - d2P - 12 - 3, 3})
                    GUI_Drawing_Panel.measureLabels.Add("A_" & mesNum + 1, {CInt(GUI.Controls("Y_TextBox").Text), pXP - d1P - 18 - 3,
                                                        pYP - d2P / 2 - 4, 3})
                Else
                    'Kvadrant 2
                    GUI_Drawing_Panel.points.Add("pA" & pointCounter * 2 - 1, {pXP - d1P, pYP, pXSW - d1SW, pYSW})
                    GUI_Drawing_Panel.points.Add("pA" & pointCounter * 2, {pXP, pYP + d2P, pXSW, pYSW - d2SW})

                    pointsMeasurements("Lmes")(1) = pointsMeasurements("Lmes")(1) + 1
                    pointsMeasurements("Wmes")(1) = pointsMeasurements("Wmes")(1) + 1

                    'Recess measuements
                    pointsMeasurements.Add("A_" & mesNum, {index + 1, index + 2, 1})
                    pointsMeasurements.Add("A_" & mesNum + 1, {index + 1, index + 2, 2})

                    'Panel recess measurements
                    GUI_Drawing_Panel.anglePoints.Add("A" & pointCounter * 2 - 1, {pXP - d1P, pYP + d2P})
                    GUI_Drawing_Panel.anglePointsComp.Add("A" & pointCounter * 2 - 1, {"pA" & pointCounter * 2 - 1, "pA" & pointCounter * 2})

                    'Label
                    GUI_Drawing_Panel.measureLabels.Add("A_" & mesNum, {CInt(GUI.Controls("Y_TextBox").Text), pXP - d1P - 18 - 3,
                                                        pYP + d2P / 2 - 4, 2})
                    GUI_Drawing_Panel.measureLabels.Add("A_" & mesNum + 1, {CInt(GUI.Controls("X_TextBox").Text), pXP - d1P / 2 - 8,
                                                        pYP + d2P + 3, 2})
                End If
            End If
            pointsFunc.Add("pA" & pointCounter * 2 - 1, False)
            pointsFunc.Add("pA" & pointCounter * 2, False)

            'Insert two new points 
            GUI_Drawing_Panel.pointsOrder.Insert(index + 1, "pA" & pointCounter * 2 - 1)
            GUI_Drawing_Panel.pointsOrder.Insert(index + 2, "pA" & pointCounter * 2)

            numOfNewPoints = 2
            angleFunctionActive = False


        Else ' square

            For i = 2 To pointsMeasurements.Count - 1
                Dim mesName As String = pointsMeasurements.Keys(i)
                If index + 1 < pointsMeasurements(mesName)(0) Then
                    pointsMeasurements(mesName)(0) = pointsMeasurements(mesName)(0) + 2
                    pointsMeasurements(mesName)(1) = pointsMeasurements(mesName)(1) + 2
                ElseIf index + 1 = pointsMeasurements(mesName)(0) Then
                    pointsMeasurements(mesName)(1) = pointsMeasurements(mesName)(1) + 2
                ElseIf index + 1 = pointsMeasurements(mesName)(1) Then
                    pointsMeasurements(mesName)(1) = pointsMeasurements(mesName)(1) + 2
                End If
            Next
            If pXP < GUI_Drawing_Panel.containerMidX Then
                If pYP > GUI_Drawing_Panel.containerMidY Then
                    'Kvadrant 4
                    GUI_Drawing_Panel.points.Add("pS" & pointCounter * 3 - 2, {pXP + d1P, pYP, pXSW + d1SW, pYSW})
                    GUI_Drawing_Panel.points.Add("pS" & pointCounter * 3 - 1, {pXP + d1P, pYP - d2P, pXSW + d1SW, pYSW + d2SW})
                    GUI_Drawing_Panel.points.Add("pS" & pointCounter * 3, {pXP, pYP - d2P, pXSW, pYSW + d2SW})

                    'Recess measuements
                    pointsMeasurements.Add("S_" & mesNum, {index + 1, index + 2, 4})
                    pointsMeasurements.Add("S_" & mesNum + 1, {index + 2, index + 3, 3})

                    'Label
                    GUI_Drawing_Panel.measureLabels.Add("S_" & mesNum, {CInt(GUI.Controls("Y_TextBox").Text), pXP + d1P + 3,
                                                        pYP - d2P / 2 - 4, 4})
                    GUI_Drawing_Panel.measureLabels.Add("S_" & mesNum + 1, {CInt(GUI.Controls("X_TextBox").Text), pXP + d1P / 2 - 8,
                                                        pYP - d2P - 12 - 3, 4})

                Else
                    'Kvadrant 1
                    GUI_Drawing_Panel.points.Add("pS" & pointCounter * 3 - 2, {pXP, pYP + d2P, pXSW, pYSW - d2SW})
                    GUI_Drawing_Panel.points.Add("pS" & pointCounter * 3 - 1, {pXP + d1P, pYP + d2P, pXSW + d1SW, pYSW - d2SW})
                    GUI_Drawing_Panel.points.Add("pS" & pointCounter * 3, {pXP + d1P, pYP, pXSW + d1SW, pYSW})

                    pointsMeasurements("Lmes")(1) = pointsMeasurements("Lmes")(1) + 2
                    pointsMeasurements("Wmes")(0) = pointsMeasurements("Wmes")(0) + 2
                    pointsMeasurements("Wmes")(1) = pointsMeasurements("Wmes")(1) + 2

                    'Recess measuements
                    pointsMeasurements.Add("S_" & mesNum, {index + 1, index + 2, 1})
                    pointsMeasurements.Add("S_" & mesNum + 1, {index + 2, index + 3, 4})

                    'Label
                    GUI_Drawing_Panel.measureLabels.Add("S_" & mesNum, {CInt(GUI.Controls("X_TextBox").Text), pXP + d1P / 2 - 8,
                                                        pYP + d2P + 3, 1})
                    GUI_Drawing_Panel.measureLabels.Add("S_" & mesNum + 1, {CInt(GUI.Controls("Y_TextBox").Text), pXP + d1P + 3,
                                                        pYP + d2P / 2 - 4, 1})
                End If
            Else
                If pYP > GUI_Drawing_Panel.containerMidY Then
                    'Kvadrant 3
                    GUI_Drawing_Panel.points.Add("pS" & pointCounter * 3 - 2, {pXP, pYP - d2P, pXSW, pYSW + d2SW})
                    GUI_Drawing_Panel.points.Add("pS" & pointCounter * 3 - 1, {pXP - d1P, pYP - d2P, pXSW - d1SW, pYSW + d2SW})
                    GUI_Drawing_Panel.points.Add("pS" & pointCounter * 3, {pXP - d1P, pYP, pXSW - d1SW, pYSW})

                    pointsMeasurements("Wmes")(1) = pointsMeasurements("Wmes")(1) + 2

                    'Recess measuements
                    pointsMeasurements.Add("S_" & mesNum, {index + 1, index + 2, 3})
                    pointsMeasurements.Add("S_" & mesNum + 1, {index + 2, index + 3, 2})

                    'Label
                    GUI_Drawing_Panel.measureLabels.Add("S_" & mesNum, {CInt(GUI.Controls("X_TextBox").Text), pXP - d1P / 2 - 8,
                                                        pYP - d2P - 12 - 3, 3})
                    GUI_Drawing_Panel.measureLabels.Add("S_" & mesNum + 1, {CInt(GUI.Controls("Y_TextBox").Text), pXP - d1P - 18 - 3,
                                                        pYP - d2P / 2 - 4, 3})
                Else
                    'Kvadrant 2
                    GUI_Drawing_Panel.points.Add("pS" & pointCounter * 3 - 2, {pXP - d1P, pYP, pXSW - d1SW, pYSW})
                    GUI_Drawing_Panel.points.Add("pS" & pointCounter * 3 - 1, {pXP - d1P, pYP + d2P, pXSW - d1SW, pYSW - d2SW})
                    GUI_Drawing_Panel.points.Add("pS" & pointCounter * 3, {pXP, pYP + d2P, pXSW, pYSW - d2SW})

                    pointsMeasurements("Lmes")(1) = pointsMeasurements("Lmes")(1) + 2
                    pointsMeasurements("Wmes")(1) = pointsMeasurements("Wmes")(1) + 2

                    'Recess measuements
                    pointsMeasurements.Add("S_" & mesNum, {index + 1, index + 2, 2})
                    pointsMeasurements.Add("S_" & mesNum + 1, {index + 2, index + 3, 1})

                    'Label
                    GUI_Drawing_Panel.measureLabels.Add("S_" & mesNum, {CInt(GUI.Controls("Y_TextBox").Text), pXP - d1P - 18 - 3,
                                                        pYP + d2P / 2 - 4, 2})
                    GUI_Drawing_Panel.measureLabels.Add("S_" & mesNum + 1, {CInt(GUI.Controls("X_TextBox").Text), pXP - d1P / 2 - 8,
                                                        pYP + d2P + 3, 2})

                End If
            End If
            pointsFunc.Add("pS" & pointCounter * 3 - 2, True)
            pointsFunc.Add("pS" & pointCounter * 3 - 1, False)
            pointsFunc.Add("pS" & pointCounter * 3, True)

            'Insert new points 
            GUI_Drawing_Panel.pointsOrder.Insert(index + 1, "pS" & pointCounter * 3 - 2)
            GUI_Drawing_Panel.pointsOrder.Insert(index + 2, "pS" & pointCounter * 3 - 1)
            GUI_Drawing_Panel.pointsOrder.Insert(index + 3, "pS" & pointCounter * 3)

            numOfNewPoints = 3

            squareFunctionActive = False

        End If
        GUI_Drawing_Panel.pointsOrder.RemoveAt(index)

        'Redraw grating
        GUI.DrawingPanel.Refresh()

        RemoveHandler GUI.Controls("X_TextBox").TextChanged, AddressOf FunctionTextCorner_Changed
        RemoveHandler GUI.Controls("X_TextBox").KeyPress, AddressOf FunctionText_KeyPress
        GUI.Controls.RemoveByKey("X_TextBox")

        RemoveHandler GUI.Controls("Y_TextBox").TextChanged, AddressOf FunctionTextCorner_Changed
        RemoveHandler GUI.Controls("Y_TextBox").KeyPress, AddressOf FunctionText_KeyPress
        GUI.Controls.RemoveByKey("Y_TextBox")

        GUI.Controls.RemoveByKey("X_Label")
        GUI.Controls.RemoveByKey("Y_Label")

        GUI.Controls("AngleButton").Enabled = True
        GUI.Controls("Button_Square").Enabled = True
        GUI.Controls("Button_SquareSide").Enabled = True
    End Sub





End Class
