Imports IntraSell_DLL

Public Class FormUtility

#Region "Splitter"


    Dim SplitterMouseCaptureChanged As Boolean = False
    Property VarName As String
    Property OriginalSplitterDistance As Integer
    Property OriginalSplitterSize As Integer

    Sub MakeSplitterCustomizable(ByRef SplitContainer1 As SplitContainer, ByRef VarName As String)
        Try
            OriginalSplitterDistance = SplitContainer1.SplitterDistance
            If SplitContainer1.Orientation = Orientation.Vertical Then
                OriginalSplitterSize = SplitContainer1.Width
            Else
                OriginalSplitterSize = SplitContainer1.Height
            End If
            If ExistsVarValue(VarName) Then
                Dim val = VarValue_Default(VarName, SplitContainer1.SplitterDistance)
                If IsNumeric(val) Then
                    SplitContainer1.SplitterDistance = val

                End If

            End If
            Me.VarName = VarName
            AddHandler SplitContainer1.MouseCaptureChanged, AddressOf SplitContainer1_MouseCaptureChanged
            AddHandler SplitContainer1.SplitterMoved, AddressOf SplitContainer1_SplitterMoved
        Catch ex As Exception
            HandleAppError(ex, Silent:=True)
        End Try
    End Sub


    Private Sub SplitContainer1_MouseCaptureChanged(sender As Object, e As EventArgs)
        SplitterMouseCaptureChanged = True
    End Sub

    Private Sub SplitContainer1_SplitterMoved(sender As Object, e As SplitterEventArgs)
        Try
            If SplitterMouseCaptureChanged Then
                Dim splitContainer = CType(sender, SplitContainer)
                Dim coef As Double
                If splitContainer.Orientation = Orientation.Vertical Then
                    coef = OriginalSplitterSize / splitContainer.Width
                Else
                    coef = OriginalSplitterSize / splitContainer.Height
                End If
                Dim recalculatedSplitterdistance = splitContainer.SplitterDistance * coef
                SetVarValue(VarName, recalculatedSplitterdistance)
                SplitterMouseCaptureChanged = False
            End If
        Catch ex As Exception
            HandleAppError(ex, Silent:=True)
        End Try
    End Sub
#End Region

#Region "Form"
    Property MyForm As Form

    Public Sub MakeFormCustomizable(form As Form)
        MyForm = form
        LoadPos()

        AddHandler form.ResizeEnd, AddressOf CrossSales_ResizeEnd
    End Sub

    Private Sub CrossSales_ResizeEnd(sender As Object, e As EventArgs)
        SavePos()
    End Sub

    Private Sub LoadPos()
        Try
            MyForm.Left = VarValue_Default(MyForm.Name & "_LEFT", MyForm.Left)
            MyForm.Top = VarValue_Default(MyForm.Name & "_TOP", MyForm.Top)
            MyForm.Width = VarValue_Default(MyForm.Name & "_WIDTH", MyForm.Width)
            MyForm.Height = VarValue_Default(MyForm.Name & "_HEIGHT", MyForm.Height)
        Catch ex As Exception
            HandleAppError(ex, Silent:=True)
        End Try
    End Sub

    Private Sub SavePos() ' Handles MyBase.Move
        Try
            SetVarValue(MyForm.Name & "_LEFT", MyForm.Left)
            SetVarValue(MyForm.Name & "_TOP", MyForm.Top)
            SetVarValue(MyForm.Name & "_WIDTH", MyForm.Width)
            SetVarValue(MyForm.Name & "_HEIGHT", MyForm.Height)
        Catch ex As Exception
            'Ignore
        End Try
    End Sub

#End Region
End Class
