Public Class MyFpoint
    Inherits FarPoint.Win.Spread.FpSpread

    Private v_AAutoAddrowInLast As ToolStripButton = Nothing
    Private V_enterToTab As Boolean = False
    Private V_DeleteKey As Boolean = False
    Private V_CurrentDel As Boolean = False
    Private V_FIRSTINDEX As Integer = 0
    Private V_NextRowindex As Integer = -1
    Private A_Ubound As New ArrayList
    Private V_SpaceEdit As Boolean = True
    Private V_sameRHeight As Boolean = True
    Private V_선택줄번호 As Int16 = -1
    <System.ComponentModel.Description("더이상데이터가없는 마지막줄일때 자동으로 해당 버튼을 클릭해줍니다.")> _
    Public Property AAutoAddrowInLast() As ToolStripButton
        Get
            Return Me.v_AAutoAddrowInLast
        End Get
        Set(ByVal value As ToolStripButton)
            Me.v_AAutoAddrowInLast = value
        End Set
    End Property
    <System.ComponentModel.Description("선택용 체크박스의 인덱스번호를 입력하세요")> _
    Public Property 선택줄번호() As Int16
        Get
            Return V_선택줄번호
        End Get
        Set(ByVal value As Int16)
            V_선택줄번호 = value
        End Set
    End Property

    <System.ComponentModel.Description("줄너비가 변경될때 모두 같이 변경됩니다")> _
    Public Property SameRowheight() As Boolean
        Get
            Return V_sameRHeight
        End Get
        Set(ByVal value As Boolean)
            V_sameRHeight = value
        End Set
    End Property



    <System.ComponentModel.Description("스페이스키를 누르면 편집모드로 들어갑니다")> _
    Public Property EditFromSpace() As Boolean
        Get
            Return V_SpaceEdit
        End Get
        Set(ByVal value As Boolean)
            V_SpaceEdit = value
        End Set
    End Property

    ''' <summary>
    ''' 중복된 데이터를 검색합니다. startindex = 시작할 줄번호,endindex = 종료할 줄번호, source 검색원본의 열번호들, target 검색대상의 열번호들
    ''' 검색완료후 전체 중복된 갯수를 리턴합니다.
    ''' </summary>
    ''' <param name="StartIndex"></param>
    ''' <param name="EndIndex"></param>
    ''' <param name="Source"></param>
    ''' <param name="Tartger"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function 중복검사(ByVal StartIndex As Integer, ByVal EndIndex As Integer, ByVal Source() As Short, ByVal Tartger() As Short, Optional ByVal CheckColnum As Short = -1, Optional ByVal PBar As ProgressBar = Nothing) As Integer
        '''''검색방법은 1번개체부터 마지막개체까지 풀루프를 돌면서 동일한 놈을 찾는다.
        Dim 원본, 대상 As String 'New System.Text.StringBuilder
        Dim fcnt As Integer = 0

        If Not PBar Is Nothing Then
            PBar.Value = StartIndex
            PBar.Minimum = StartIndex
            PBar.Maximum = EndIndex
        End If

        'NOTICE("중복되는 [전주번호]를 찾는중입니다...", Me.lb_msg)
        'Me.Invalidate()


        For A As Integer = StartIndex To EndIndex
            원본 = ""
            For Each T As Short In Source
                원본 &= Me.ActiveSheet.Cells(A, T).Value
            Next

            If 원본.ToString <> "" Then
                For B As Integer = StartIndex To EndIndex
                    If A <> B Then   '//같은라인은 피하고 그렇지 않은 라인의 번호를 확인한다.
                        대상 = ""
                        For Each T As Short In Tartger
                            대상 &= Me.ActiveSheet.Cells(B, T).Value
                        Next
                        If 원본.ToString = 대상.ToString Then
                            'MsgBox(원본)

                            fcnt += 1
                            If CheckColnum > -1 Then
                                Me.ActiveSheet.Cells(A, CheckColnum).Value = True
                                Me.ActiveSheet.Cells(B, CheckColnum).Value = True
                            End If
                        End If
                    End If
                Next
            End If
            If Not PBar Is Nothing Then
                If PBar.Value < PBar.Maximum Then PBar.Value += 1
                Me.Invalidate()
            End If
        Next
        Return fcnt

        'NOTICE("[전주번호] 중복검사 완료(" & fcnt & "개의 데이터를 찾았습니다)", Me.lb_msg)
    End Function


    ''' <summary>
    ''' 중복된 데이터를 검색합니다. startindex = 시작할 줄번호,endindex = 종료할 줄번호, source 검색원본의 열번호들, target 검색대상의 열번호들
    ''' 검색완료후 전체 중복된 갯수를 리턴합니다.
    ''' </summary>
    ''' <param name="StartIndex"></param>
    ''' <param name="EndIndex"></param>
    ''' <param name="Source"></param>
    ''' <param name="Tartger"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function 중복검사2(ByVal StartIndex As Integer, ByVal EndIndex As Integer, ByVal Source() As Short, ByVal Tartger() As Short, Optional ByVal CheckColnum As Short = -1, Optional ByVal PBar As ProgressBar = Nothing) As Integer
        '''''검색방법은 1번개체부터 마지막개체까지 풀루프를 돌면서 동일한 놈을 찾는다.
        Dim 원본, 대상 As String 'New System.Text.StringBuilder
        Dim fcnt As Integer = 0

        If Not PBar Is Nothing Then
            PBar.Value = StartIndex
            PBar.Minimum = StartIndex
            PBar.Maximum = EndIndex
        End If

        For A As Integer = StartIndex To EndIndex
            원본 = ""
            For Each T As Short In Source
                원본 &= Me.ActiveSheet.Cells(A, T).Value
            Next
            If 원본.Trim.ToString <> "" Then
                For B As Integer = StartIndex To EndIndex
                    If A <> B Then   '//같은라인은 피하고 그렇지 않은 라인의 번호를 확인한다.
                        대상 = ""
                        For Each T As Short In Tartger
                            대상 &= Me.ActiveSheet.Cells(B, T).Value
                        Next
                        If 원본.Trim.ToString = 대상.Trim.ToString Then
                            fcnt += 1
                            If CheckColnum > -1 Then
                                Me.ActiveSheet.Cells(A, CheckColnum).Value = True
                                Me.ActiveSheet.Cells(B, CheckColnum).Value = True
                            End If
                        End If
                    End If
                Next
            End If
            If Not PBar Is Nothing Then
                If PBar.Value < PBar.Maximum Then PBar.Value += 1
                Me.Invalidate()
            End If
        Next
        Return fcnt
        'NOTICE("[전주번호] 중복검사 완료(" & fcnt & "개의 데이터를 찾았습니다)", Me.lb_msg)
    End Function

    Public Function 중복검사같은줄(ByVal StartIndex As Integer, ByVal EndIndex As Integer, ByVal Source() As Short, ByVal Tartger() As Short, Optional ByVal CheckColnum As Short = -1, Optional ByVal PBar As ProgressBar = Nothing, Optional ByVal TrueDirection As Boolean = True) As Integer
        Dim 원본, 대상 As String 'New System.Text.StringBuilder
        Dim fcnt As Integer = 0
        Dim Can As Boolean = False
        If Not PBar Is Nothing Then : PBar.Value = StartIndex : PBar.Minimum = StartIndex : PBar.Maximum = EndIndex : End If

        For A As Integer = StartIndex To EndIndex
            원본 = "" : 대상 = ""
            Can = False
            For Each T As Short In Source
                원본 &= Me.ActiveSheet.Cells(A, T).Value
            Next
            For Each T As Short In Tartger
                대상 &= Me.ActiveSheet.Cells(A, T).Value
            Next

            If 원본.Trim.ToString <> "" Then
                If TrueDirection = True Then
                    If 원본.Trim.ToString = 대상.Trim.ToString Then Can = True
                Else
                    If 원본.Trim.ToString <> 대상.Trim.ToString Then Can = True
                End If
                If Can Then
                    fcnt += 1
                    If CheckColnum > -1 Then Me.ActiveSheet.Cells(A, CheckColnum).Value = True
                End If

            End If
            If Not PBar Is Nothing Then
                If PBar.Value < PBar.Maximum Then PBar.Value += 1
                Me.Invalidate()
            End If
        Next
        Return fcnt

    End Function

    Public Function 중복검사같은줄공란포함(ByVal StartIndex As Integer, ByVal EndIndex As Integer, ByVal Source() As Short, ByVal Tartger() As Short, Optional ByVal CheckColnum As Short = -1, Optional ByVal PBar As ProgressBar = Nothing, Optional ByVal TrueDirection As Boolean = True) As Integer
        Dim 원본, 대상 As String 'New System.Text.StringBuilder
        Dim fcnt As Integer = 0
        Dim Can As Boolean = False
        If Not PBar Is Nothing Then : PBar.Value = StartIndex : PBar.Minimum = StartIndex : PBar.Maximum = EndIndex : End If

        For A As Integer = StartIndex To EndIndex
            원본 = "" : 대상 = ""
            Can = False
            For Each T As Short In Source
                원본 &= Me.ActiveSheet.Cells(A, T).Text
            Next
            For Each T As Short In Tartger
                대상 &= Me.ActiveSheet.Cells(A, T).Text
            Next

            If TrueDirection = True Then
                If 원본.Trim.ToString = 대상.Trim.ToString Then Can = True
            Else
                If 원본.Trim.ToString <> 대상.Trim.ToString Then Can = True
            End If

            If Can Then
                fcnt += 1
                If CheckColnum > -1 Then Me.ActiveSheet.Cells(A, CheckColnum).Value = True
            End If

            If Not PBar Is Nothing Then
                If PBar.Value < PBar.Maximum Then PBar.Value += 1
                Me.Invalidate()
            End If
        Next
        Return fcnt

    End Function

    ''' <summary>
    ''' 사용하지 않는걸 권장합니다( 이명령은  insertNewrow 로 사용하길 권장합니다.)
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function AddRowsU() As Integer
        Dim Idx As Integer
        With Me.ActiveSheet
            If .RowCount <= 0 Then  '//데이터가 아에없다면
                Idx = 0
                .AddUnboundRows(Idx, 1)
                .Rows(Idx).Tag = "UNBOUND"
                .ActiveRowIndex = 0
                .ActiveColumnIndex = Me.FIRST_FOCUSINDEX
                A_Ubound.Add(Idx)
            Else
                Idx = IIf(.ActiveRow.Index < 0, 0, .ActiveRow.Index)
                .AddUnboundRows(Idx, 1)
                .Rows(Idx).Tag = "UNBOUND"
                A_Ubound.Add(Idx)
            End If
        End With
        Return Idx
    End Function

    ''' <summary>
    ''' 현재위치 위에 새로운 행을 추가합니다(바운드 적용됨)
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub Arin_InsertNewRow()
        If Not Me.Focused Then Me.Focus()
        Me.ActiveSheet.Rows.Add(Me.ActiveSheet.ActiveRowIndex, 1)
        Me.ActiveSheet.SetActiveCell(Me.ActiveSheet.ActiveRowIndex, Me.FIRST_FOCUSINDEX)
    End Sub

    ''' <summary>
    ''' 마지막에 새로운 행을 추가합니다(바운드 적용됨)
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub Arin_AddNewRow()
        If Not Me.Focused Then Me.Focus()
        Me.ActiveSheet.Rows.Add(Me.ActiveSheet.RowCount, 1)
        Me.ActiveSheet.SetActiveCell(Me.ActiveSheet.RowCount, Me.FIRST_FOCUSINDEX)
        SendKeys.Send("{ENTER}")
    End Sub

    ''' <summary>
    ''' 마지막에 새로운 행을 추가합니다.(바운드적용안됨)
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub Arin_AddNewRowU()
        If Not Me.Focused Then Me.Focus()
        Dim Idx As Integer
        Me.ActiveSheet.AddUnboundRows(Me.ActiveSheet.RowCount, 1)
        Idx = Me.ActiveSheet.RowCount - 1
        Me.ActiveSheet.Rows(Idx).Tag = "UNBOUND"
        Me.ActiveSheet.SetActiveCell(Me.ActiveSheet.RowCount, Me.FIRST_FOCUSINDEX)
        SendKeys.Send("{ENTER}")
    End Sub

    Public Function NewRowsU() As Integer
        Dim Idx As Integer
        Me.ActiveSheet.AddUnboundRows(Me.ActiveSheet.RowCount, 1)
        Idx = Me.ActiveSheet.RowCount - 1
        Me.ActiveSheet.Rows(Idx).Tag = "UNBOUND"
        A_Ubound.Add(Idx)    '--언바운드목록에 제외시켜버린다
        Return Idx
    End Function
    Public Sub Attach_Ubound(Optional ByVal NullChkColnum As Integer = -1)
        ' MsgBox(A_Ubound.Count)
        Dim nullVal As Boolean = False
        For Each A As Integer In A_Ubound
            Try
                If Me.ActiveSheet.Rows(CInt(A)).Tag = "UNBOUND" Then    '//언바운드된열일경우에만
                    If NullChkColnum > -1 Then
                        nullVal = True
                        For z As Integer = NullChkColnum To Me.ActiveSheet.ColumnCount - 1
                            If Me.ActiveSheet.Cells(A, z).Text <> "" Then
                                'MsgBox(Me.ActiveSheet.Cells(A, z).Text)
                                nullVal = False
                                Exit For
                            End If
                        Next
                    End If
                    If Not nullVal Then Me.ActiveSheet.AddRowToDataSource(CInt(A), True)
                End If
            Catch ex As Exception
                MsgBox("[Err]Add-Unbound Row" & vbCrLf & ex.Message.ToString)
            End Try
        Next
        A_Ubound.Clear()
    End Sub

    <System.ComponentModel.Description("줄바꿈을 할 열 번호입니다")> _
    Public Property NextRow_Index() As Integer
        Get
            Return V_NextRowindex

        End Get
        Set(ByVal value As Integer)
            V_NextRowindex = value
        End Set
    End Property

    <System.ComponentModel.Description("줄이 바뀌었을때 커서가 위치할 시작 값입니다.")> _
    Public Property FIRST_FOCUSINDEX() As Integer
        Get
            Return V_FIRSTINDEX

        End Get
        Set(ByVal value As Integer)
            V_FIRSTINDEX = value
        End Set
    End Property

    <System.ComponentModel.Description("Del 키를 누를시 커서가 위치한 셀의 값을 초기합니다")> _
    Public Property CurrentDel() As Boolean
        Get
            Return V_CurrentDel

        End Get
        Set(ByVal value As Boolean)
            V_CurrentDel = value
        End Set
    End Property


    <System.ComponentModel.Description("Shift+Del 키를 누를시 해당 줄을 삭제합니다.")> _
    Public Property DeleteKey() As Boolean
        Get
            Return V_DeleteKey

        End Get
        Set(ByVal value As Boolean)
            V_DeleteKey = value
        End Set
    End Property

    ''' <summary>
    ''' 입력된 줄번호와 태그=데이터필드 가 일치하는 셀을 반환합니다.
    ''' </summary>
    ''' <param name="index"></param>
    ''' <param name="Tag"></param>
    ''' <param name="Sheetindex"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function Cells(ByVal index As Integer, ByVal Tag As Object, Optional ByVal Sheetindex As Integer = -1) As FarPoint.Win.Spread.Cell
        Dim RetVal As FarPoint.Win.Spread.Cell = Nothing
        If Sheetindex = -1 Then Sheetindex = Me.ActiveSheetIndex

        For Each SC As FarPoint.Win.Spread.Column In Me.Sheets(Sheetindex).Columns

            If SC.DataField.ToUpper = Tag.ToString.ToUpper Then
                RetVal = Me.Sheets(Sheetindex).Cells(index, SC.Index)
                Exit For
            End If
        Next
        If RetVal Is Nothing Then
            MsgBox("Can't Find column [" & Tag.ToString & "]", MsgBoxStyle.Critical, "Farpoint function Cells")
        End If
        Return RetVal
    End Function
    ''' <summary>
    ''' 현재선택된 줄의 열번호에해당하는 셀을 반환합니다.
    ''' </summary>
    ''' <param name="Columnindex"></param>
    ''' <param name="Sheetindex"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function Cells(ByVal Columnindex As Integer, Optional ByVal Sheetindex As Integer = -1) As FarPoint.Win.Spread.Cell
        Dim RetVal As FarPoint.Win.Spread.Cell = Nothing
        If Sheetindex = -1 Then Sheetindex = Me.ActiveSheetIndex
        Return Me.Sheets(Sheetindex).Cells(Me.ActiveSheet.ActiveRowIndex, Columnindex)
        Return RetVal
    End Function
    ''' <summary>
    ''' 현재선택된 셀을 반환합니다
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function Cells() As FarPoint.Win.Spread.Cell
        Dim RetVal As FarPoint.Win.Spread.Cell = Nothing
        Return Me.ActiveSheet.ActiveCell
    End Function
    Public Function ActiveCell() As FarPoint.Win.Spread.Cell
        Dim RetVal As FarPoint.Win.Spread.Cell = Nothing
        Return Me.ActiveSheet.ActiveCell
    End Function
    ''' <summary>
    ''' 전줄의 현재열위치 셀을 반환
    ''' </summary>
    ''' <param name="FirstrowValueisNothing"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function PreRCells(Optional ByVal FirstrowValueisNothing As Boolean = False) As FarPoint.Win.Spread.Cell
        If FirstrowValueisNothing AndAlso Me.ActiveRowindex = 0 Then Return Nothing

        If Me.ActiveRowindex = 0 Then
            If FirstrowValueisNothing Then
                Return Nothing
            Else
                Return Me.ActiveSheet.ActiveCell
            End If
        Else
            Return Me.ActiveSheet.Cells(Me.ActiveRowindex - 1, Me.ActiveColumnindex)
        End If
    End Function
    ''' <summary>
    ''' 앞셀을 반환
    ''' </summary>
    ''' <param name="FirstColValueisNothing"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function PreCells(Optional ByVal FirstColValueisNothing As Boolean = False) As FarPoint.Win.Spread.Cell
        If FirstColValueisNothing AndAlso Me.ActiveColumnindex = 0 Then Return Nothing

        If Me.ActiveColumnindex = 0 Then
            If FirstColValueisNothing Then
                Return Nothing
            Else
                Return Me.ActiveSheet.ActiveCell
            End If
        Else
            Return Me.ActiveSheet.Cells(Me.ActiveRowindex, Me.ActiveColumnindex - 1)
        End If
    End Function
    ''' <summary>
    ''' 입력된 태그와 데이터필드가 일치하는 셀을 반환합니다.(줄번호는 현재줄)
    ''' </summary>
    ''' <param name="Tag"></param>
    ''' <param name="Sheetindex"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function Cells(ByVal Tag As Object, Optional ByVal Sheetindex As Integer = -1) As FarPoint.Win.Spread.Cell
        Dim RetVal As FarPoint.Win.Spread.Cell = Nothing
        If Sheetindex = -1 Then Sheetindex = Me.ActiveSheetIndex

        For Each SC As FarPoint.Win.Spread.Column In Me.Sheets(Sheetindex).Columns

            If SC.DataField.ToUpper = Tag.ToString.ToUpper Then
                RetVal = Me.Sheets(Sheetindex).Cells(Me.ActiveRowindex, SC.Index)
                Exit For
            End If
        Next
        If RetVal Is Nothing Then
            MsgBox("Can't Find column [" & Tag.ToString & "]", MsgBoxStyle.Critical, "Farpoint function Cells")
        End If
        Return RetVal
    End Function
    ''' <summary>
    ''' 활성화된 열제목줄을 반환합니다.
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function ActiveColumn() As FarPoint.Win.Spread.Column
        Return Me.ActiveSheet.Columns(Me.ActiveColumnindex)
    End Function
    ''' <summary>
    ''' 입력된 태그와 데이터필드가 일치하는 셀을 반환합니다.(줄번호는 현재줄)
    ''' </summary>
    ''' <param name="Tag"></param>
    ''' <param name="Sheetindex"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function CCells(ByVal Tag As Object, Optional ByVal Sheetindex As Integer = -1) As FarPoint.Win.Spread.Cell
        Dim RetVal As FarPoint.Win.Spread.Cell = Nothing
        If Sheetindex = -1 Then Sheetindex = Me.ActiveSheetIndex

        For Each SC As FarPoint.Win.Spread.Column In Me.Sheets(Sheetindex).Columns

            If SC.DataField.ToUpper = Tag.ToString.ToUpper Then
                RetVal = Me.Sheets(Sheetindex).Cells(Me.ActiveRowindex, SC.Index)
                Exit For
            End If
        Next
        If RetVal Is Nothing Then
            MsgBox("Can't Find column [" & Tag.ToString & "]", MsgBoxStyle.Critical, "Farpoint function Cells")
        End If
        Return RetVal
    End Function

    ''' <summary>
    ''' 활성화된 줄번호를 반환
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function ActiveRowindex() As Integer
        Return Me.ActiveSheet.ActiveRowIndex
    End Function
    ''' <summary>
    ''' 활성화된 열버호를 반환
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function ActiveColumnindex() As Integer
        Return Me.ActiveSheet.ActiveColumnIndex
    End Function

    ''' <summary>
    ''' 엔터키를 누르면 다음셀로이동하는 키맵을 설정합니다.
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub EnterToNextItem()
        Dim im As New FarPoint.Win.Spread.InputMap
        im = Me.GetInputMap(FarPoint.Win.Spread.InputMapMode.WhenAncestorOfFocused)
        im.Put(New FarPoint.Win.Spread.Keystroke(Keys.Enter, Keys.None), FarPoint.Win.Spread.SpreadActions.MoveToNextColumnWrap)
        im = Me.GetInputMap(FarPoint.Win.Spread.InputMapMode.WhenFocused)
        im.Put(New FarPoint.Win.Spread.Keystroke(Keys.Enter, Keys.None), FarPoint.Win.Spread.SpreadActions.MoveToNextColumnWrap)
    End Sub

    Public Sub NEXT_CELL()
        If Me.ActiveSheet.ActiveRowIndex <> Me.ActiveSheet.RowCount - 1 Then '//마지막줄이 아니면
            If Me.ActiveSheet.ActiveColumnIndex = Me.ActiveSheet.ColumnCount - 1 Then '/마지막열이면 다음줄 첨으로 이동
                'SendKeys.Send("{TAB}")
                Me.ActiveSheet.ActiveRowIndex += 1
                Me.ActiveSheet.ActiveColumnIndex = V_FIRSTINDEX '//첫인덱스로 옴긴다.기본값은 0이다.
                'Me.ActiveSheet.SetActiveCell(Me.ActiveSheet.ActiveRowIndex + 1, V_FIRSTINDEX)

            Else
                'SendKeys.Send("{TAB}")
                'Me.ActiveSheet.SetActiveCell(Me.ActiveSheet.ActiveRowIndex, Me.ActiveSheet.ActiveColumnIndex + 1)
                'Me.ActiveSheet.IsSelected(Me.ActiveSheet.ActiveRowIndex, Me.ActiveSheet.ActiveColumnIndex + 1)
                'ME.ActiveSheet.SEL
                Me.ActiveSheet.ActiveColumnIndex += 1
            End If
        Else
            If Me.ActiveSheet.ActiveColumnIndex <> Me.ActiveSheet.ColumnCount - 1 Then '/마지막열이면 다음줄 첨으로 이동
                'Me.ActiveSheet.ActiveRowIndex += 1
                Me.ActiveSheet.ActiveColumnIndex += 1 '//첫인덱스로 옴긴다.기본값은 0이다.
            End If
        End If
    End Sub
    Public Sub NEXT_ROW()
        If Me.ActiveSheet.ActiveRowIndex <> Me.ActiveSheet.RowCount - 1 Then '//마지막줄이 아니면
            Me.ActiveSheet.ActiveRowIndex += 1
            Me.ActiveSheet.ActiveColumnIndex = V_FIRSTINDEX '//첫인덱스로 옴긴다.기본값은 0이다.        
        End If
    End Sub

    Public Sub ItemCheck_Select(Optional ByVal index As Int16 = -1)

        Dim sheet As FarPoint.Win.Spread.SheetView = Me.ActiveSheet
        If index = -1 AndAlso Me.선택줄번호 <> -1 Then index = 선택줄번호 '//지정된열제목이 잇는지
        sheet.AutoFilterReset(index)   '//필터적용시 만힝 느리므로 필터를 해제한다.
        For i As Integer = 0 To sheet.RowCount - 1
            sheet.Cells(i, index).Value = True
        Next
    End Sub
    Public Sub ItemCheck_Unselect(Optional ByVal index As Int16 = -1)
        Dim sheet As FarPoint.Win.Spread.SheetView = Me.ActiveSheet
        If index = -1 AndAlso Me.선택줄번호 <> -1 Then index = 선택줄번호 '//지정된열제목이 잇는지
        sheet.AutoFilterReset(index)   '//필터적용시 만힝 느리므로 필터를 해제한다.
        For i As Integer = 0 To sheet.RowCount - 1
            sheet.Cells(i, index).Value = False
        Next
    End Sub
    Public Sub ItemCheck_Reverse(Optional ByVal index As Int16 = -1)
        Dim sheet As FarPoint.Win.Spread.SheetView = Me.ActiveSheet
        If index = -1 AndAlso Me.선택줄번호 <> -1 Then index = 선택줄번호 '//지정된열제목이 잇는지
        sheet.AutoFilterReset(index)   '//필터적용시 만힝 느리므로 필터를 해제한다.
        For i As Integer = 0 To sheet.RowCount - 1
            sheet.Cells(i, index).Value = IIf(sheet.Cells(i, index).Value = True, False, True)
        Next
    End Sub
    ''' <summary>
    ''' 뷰의 열너비,열이름(옵션),줌팩터등을 저장합니다.
    ''' </summary>
    ''' <param name="ColName"></param>
    ''' <remarks></remarks>
    Public Sub Setting_Save(Optional ByVal ColName As Boolean = True, Optional ByVal Tag As String = "", Optional ByVal FN As String = "")
        ColSize_Save(Tag, FN)
        If ColName Then ColName_Save(Tag, FN)
        CurrentState_Save(Tag, FN)
        AZOOM_Save(Tag, FN)
    End Sub
    ''' <summary>
    ''' 뷰의 열너비,열이름(옵션),줌팩터등을 불러옵니다
    ''' </summary>
    ''' <param name="ColName"></param>
    ''' <remarks></remarks>
    Public Sub Setting_Load(Optional ByVal ColName As Boolean = True, Optional ByVal Tag As String = "", Optional ByVal FN As String = "")
        ColSize_Load(Tag, FN)
        If ColName Then ColName_Load(Tag, FN)
        CurrentState_Load(Tag, FN)
        AZOOM_LOAD(Tag, FN)
    End Sub

    ''' <summary>
    ''' 현재 열너비를 Spread.ini 파일에 저장합니다.
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub AZOOM_Save(ByVal tag As String, ByVal fn As String)
        Dim File As New System.IO.FileInfo(My.Application.Info.DirectoryPath & "\" & fn & "spread.INI")
        If System.IO.Directory.Exists(File.DirectoryName) = False Then System.IO.Directory.CreateDirectory(File.DirectoryName)
        Dim a As New MyINI2(File.FullName)
        a.Write(Me.Parent.Name & "-" & (Me.Name & tag), "ZOOM", Me.ActiveSheet.ZoomFactor)

    End Sub
    ''' <summary>
    ''' 현재 열너비를 Spread.ini 파일로 부터 불러옵니다.
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub AZOOM_LOAD(ByVal tag As String, ByVal fn As String)
        Dim FILEname As String = My.Application.Info.DirectoryPath & "\" & fn & "spread.INI"
        If Not System.IO.File.Exists(FILEname) Then
            MsgBox("열너비 파일이 존재하지 않습니다", MsgBoxStyle.Critical, "열너비설정-오류")
            ColSize_Save(tag, fn)
            Return
        End If
        'MsgBox("PARENT" & Me.Parent.Name.ToString & "/" & Me.Parent.Text)
        Dim a As New MyINI2(FILEname)
        Me.ActiveSheet.ZoomFactor = a.Read(Me.Parent.Name & "-" & (Me.Name & tag), "ZOOM", 1)
    End Sub

    ''' <summary>
    ''' 현재 열너비를 Spread.ini 파일에 저장합니다.
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub ColSize_Save(ByVal tag As String, ByVal fn As String)
        Dim File As New System.IO.FileInfo(My.Application.Info.DirectoryPath & "\" & fn & "spread.INI")
        If System.IO.Directory.Exists(File.DirectoryName) = False Then System.IO.Directory.CreateDirectory(File.DirectoryName)
        Dim a As New MyINI2(File.FullName)
        For Each z As FarPoint.Win.Spread.Column In Me.ActiveSheet.Columns
            a.Write(Me.Parent.Name & "-" & (Me.Name & tag), "COLSIZE" & z.Index.ToString, z.Width)
        Next
    End Sub

    ''' <summary>
    ''' 현재 열너비를 Spread.ini 파일로 부터 불러옵니다.
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub ColSize_Load(ByVal tag As String, ByVal fn As String)
        Dim FILEname As String = My.Application.Info.DirectoryPath & "\" & fn & "spread.INI"
        If Not System.IO.File.Exists(FILEname) Then
            MsgBox("열너비 파일이 존재하지 않습니다", MsgBoxStyle.Critical, "열너비설정-오류")
            ColSize_Save(tag, fn)
            Return
        End If
        'MsgBox("PARENT" & Me.Parent.Name.ToString & "/" & Me.Parent.Text)
        Dim a As New MyINI2(FILEname)
        For Each col As FarPoint.Win.Spread.Column In Me.ActiveSheet.Columns
            'MsgBox(COL.Index)
            'a.Write(Me.Parent.Name, "COLSIZE" & COL.Index.ToString, File, COL.Width)
            Try
                col.Width = a.Read(Me.Parent.Name & "-" & (Me.Name & tag), "COLSIZE" & col.Index.ToString)
            Catch ex As Exception   '//없을경우에는 에러를 내므로 그냥 나간다

            End Try
        Next
    End Sub
    Public Sub ColName_Load(ByVal tag As String, ByVal fn As String)
        Dim FILEname As String = My.Application.Info.DirectoryPath & "\" & fn & "spread.INI"

        If Not System.IO.File.Exists(FILEname) Then
            MsgBox("열너비 파일이 존재하지 않습니다", MsgBoxStyle.Critical, "열너비설정-오류")
            ColSize_Save(tag, fn)
            Return
        End If
        'MsgBox("PARENT" & Me.Parent.Name.ToString & "/" & Me.Parent.Text)
        Dim a As New MyINI2(FILEname)
        For Each col As FarPoint.Win.Spread.Column In Me.ActiveSheet.Columns
            'MsgBox(COL.Index)
            'a.Write(Me.Parent.Name, "COLSIZE" & COL.Index.ToString, File, COL.Width)
            Try
                col.Label = a.Read(Me.Parent.Name & "-" & Me.Name & tag, "COLNAME" & col.Index.ToString).ToString.Trim(Chr(0))
            Catch ex As Exception   '//없을경우에는 에러를 내므로 그냥 나간다
            End Try
        Next
    End Sub
    Public Sub ColName_Save(ByVal tag As String, ByVal fn As String)
        Dim File As New System.IO.FileInfo(My.Application.Info.DirectoryPath & "\" & fn & "spread.INI")
        If System.IO.Directory.Exists(File.DirectoryName) = False Then System.IO.Directory.CreateDirectory(File.DirectoryName)
        Dim a As New MyINI2(File.FullName)
        For Each z As FarPoint.Win.Spread.Column In Me.ActiveSheet.Columns
            a.Write(Me.Parent.Name & "-" & Me.Name & tag, "COLNAME" & z.Index.ToString, z.Label)
        Next
    End Sub

    ''' <summary>
    ''' [보충요망] :줄높이를 저장
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub CurrentState_Save(ByVal tag As String, ByVal fn As String)
        Dim File As New System.IO.FileInfo(My.Application.Info.DirectoryPath & "\" & fn & "spread.INI")
        If System.IO.Directory.Exists(File.DirectoryName) = False Then System.IO.Directory.CreateDirectory(File.DirectoryName)
        Dim a As New MyINI2(File.FullName)
        a.Write(Me.Parent.Name & "-" & Me.Name & tag, "RowHeight", Me.ActiveSheet.Rows.Default.Height)
    End Sub

    ''' <summary>
    ''' [보충요망] :줄높이를 불러온다.(spre.ini)
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub CurrentState_Load(ByVal tag As String, ByVal fn As String)
        Dim FILEname As String = My.Application.Info.DirectoryPath & "\" & fn & "spread.INI"
        If Not System.IO.File.Exists(FILEname) Then
            MsgBox("열너비 파일이 존재하지 않습니다", MsgBoxStyle.Critical, "열너비설정-오류")
            CurrentState_Save(tag, fn)
            Return
        End If
        Dim a As New MyINI2(My.Application.Info.DirectoryPath & "\" & fn & "spread.INI")
        Me.ActiveSheet.Rows.Default.Height = a.Read(Me.Parent.Name & "-" & Me.Name & tag, "RowHeight", 20)
    End Sub


    Private Sub Delete_itm(Optional ByVal rowindex As Integer = -1, Optional ByVal prompt As Boolean = True)
        Dim Commit As Boolean = True
        Dim A As New System.Text.StringBuilder("경고 : 데이터 삭제를 선택했습니다")
        A.AppendLine()
        A.AppendLine("현재 선택된 줄이 삭제됩니다")
        A.AppendLine("계속하시겠습니까?")
        If prompt Then
            If MsgBox(A.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkCancel, "선택줄 삭제") <> MsgBoxResult.Ok Then Commit = False
        End If
        If Not Commit Then Return '//명령프롬프트에서 취소를 선택햇을시에는  빠져나간다.
        Me.ActiveSheet.ActiveRow.Remove()
    End Sub

    Private Sub MyFpoint_EditModeOff(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.EditModeOff


    End Sub

    ''' <summary>
    ''' 전줄의 현재열위치 셀을 반환
    ''' </summary>
    ''' <param name="FirstrowValueisNothing"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function CellsPR(Optional ByVal FirstrowValueisNothing As Boolean = False) As FarPoint.Win.Spread.Cell
        If FirstrowValueisNothing AndAlso Me.ActiveRowindex = 0 Then Return Nothing

        If Me.ActiveRowindex = 0 Then
            If FirstrowValueisNothing Then
                Return Nothing
            Else
                Return Me.ActiveSheet.ActiveCell
            End If
        Else
            Return Me.ActiveSheet.Cells(Me.ActiveRowindex - 1, Me.ActiveColumnindex)
        End If
    End Function

    Private Sub CustFP_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        e.Handled = True
        e.SuppressKeyPress = False

        Select Case e.KeyCode
            Case Keys.Enter
                If e.Control Then
                    Me.Cells.Value = Me.CellsPR.Value
                Else
                    If Not Me.v_AAutoAddrowInLast Is Nothing Then
                        If Me.ActiveSheet.ActiveRowIndex = Me.ActiveSheet.RowCount - 1 Then '//마지막줄에
                            If Me.V_NextRowindex = Me.ActiveSheet.ActiveColumnIndex Then '//다음줄로가아햘 경우라면
                                e.Handled = True
                                Me.v_AAutoAddrowInLast.PerformClick()
                            ElseIf Me.ActiveSheet.ActiveColumnIndex = Me.ActiveSheet.ColumnCount - 1 Then   '//마지막열이라면
                                e.Handled = True
                                Me.v_AAutoAddrowInLast.PerformClick()
                            End If
                        End If
                    Else
                        If Me.V_NextRowindex = Me.ActiveSheet.ActiveColumnIndex Then '//다음줄로가아햘 경우라면
                            Me.NEXT_ROW()
                        ElseIf Me.ActiveSheet.ActiveColumnIndex = Me.ActiveSheet.ColumnCount - 1 Then   '//마지막열이라면
                            Me.NEXT_ROW()
                        End If
                    End If
                End If
            Case Keys.Delete

                If e.Shift Then '//쉬프트가 눌러져잇을경우
                    If Me.V_DeleteKey Then Delete_itm(, True)
                    e.Handled = True
                Else
                    If Me.V_CurrentDel Then
                        Me.ActiveSheet.ActiveCell.ResetValue()
                        Me.OnEditModeOff(Nothing)
                        e.Handled = True
                    End If
                End If

            Case Keys.Space
                If Me.V_SpaceEdit Then EditMode = True
                e.Handled = True
            Case Else '//그외다른게입력되어있을경우에는 editmode 에 들어가게되므로 한글처리를 위해 바로 에디트모드로 들어오게한다.

                If e.KeyCode >= Keys.NumPad0 AndAlso e.KeyCode <= Keys.NumPad9 Then
                    '//우측숫자패트 
                ElseIf e.KeyCode >= Keys.D0 AndAlso e.KeyCode <= Keys.D9 Then
                    '//일반숫자패드
                Else
                    If Not e.Alt AndAlso Not e.Control AndAlso Me.ImeMode = Windows.Forms.ImeMode.Hangul Then
                        EditMode = True
                    End If
                End If

        End Select
    End Sub

    Private Sub CustFP_RowHeightChanged(ByVal sender As Object, ByVal e As FarPoint.Win.Spread.RowHeightChangedEventArgs) Handles Me.RowHeightChanged
        If Me.V_sameRHeight Then
            Dim Row As FarPoint.Win.Spread.RowHeightChangeExtents = e.RowList(0)
            Me.ActiveSheet.Rows.Default.Height = Me.ActiveSheet.Rows(Row.FirstRow).Height
        End If

    End Sub

End Class
