Imports System.Text
Imports System.IO
Public Class MyListView
    Private AutoView As Boolean = False '//자동으로 리뷰의내용을 쿼리블럭에표시할지
    Private var_AutoDelete As Boolean = True  '//Delte키를 눌렀을때 자동으로 rowid를 이용해서 삭제한다.
    Private VAR_ENABLE_MYKEY As Boolean = True
    Private var_enterkey As Boolean = True


    ''' <summary>
    ''' 엔터키를 누르면 포커스를 이동합니까? 
    ''' </summary>
    ''' <remarks></remarks>
    <System.ComponentModel.Description("엔터키를 누르면 다음으로 포커스를 이동합니까?")> _
    Public Property DB_Enable_EnterKey() As Boolean
        Get
            Return var_enterkey
        End Get
        Set(ByVal value As Boolean)
            var_enterkey = value
        End Set
    End Property

    ''' <summary>
    ''' 이 개체에 커서를 이동합니다.
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub DB_SetFocus()
        If Me.Items.Count <= 0 Then
            MsgBox("뷰 데이터가 존재하지 않습니다", MsgBoxStyle.Information, "확인")
        Else
            Me.Focus()
        End If
    End Sub

    ''' <summary>
    ''' 기본 셋트된 키 ENTER/SHIFT+KEY/DELETE 를 비활성화합니다.
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    <System.ComponentModel.Description("자동넘김 ENTER/SHIFT+ENTER/DELETE 키를 비활성화합니다")> _
    Public Property DB_Enable_MyKey() As Boolean
        Get
            Return (VAR_ENABLE_MYKEY)
        End Get
        Set(ByVal value As Boolean)
            VAR_ENABLE_MYKEY = value
        End Set
    End Property


    ''' <summary>
    ''' DELETE 키가 눌렷을때 선택되아이템의 0번재 값을 ROWID로 삭제합니다
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    <System.ComponentModel.Description("Delete키를 눌렀을때 0번째 ID값을 가지고 삭제합니다")> _
    Public Property DB_Enable_DelKey() As Boolean
        Get
            Return var_AutoDelete
        End Get
        Set(ByVal value As Boolean)
            var_AutoDelete = value
        End Set
    End Property

    ''' <summary>
    ''' 자동으로 리뷰의 내용을 설정된 쿼리블럭에 표시합니다 selectedindexchanged 함수가 호출됩니다.
    ''' </summary>
    ''' <value></value>
    ''' <remarks></remarks>
    <System.ComponentModel.Description("자동으로 리뷰의 내용을 설정된 쿼리블럭에 표시합니다 seletecindexchanged 함수가 호출됩니다")> _
    Public Property DB_Autoview() As Boolean
        Get
            Return AutoView
        End Get
        Set(ByVal value As Boolean)
            AutoView = value
        End Set
    End Property


    ''' <summary>
    ''' 모든 개체를 선택합니다.
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub ITEM_SELECT()
        For Each A As ListViewItem In Me.Items
            A.Checked = True
        Next
    End Sub

    ''' <summary>
    ''' 모든 개체의 선택을 해제합니다.
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub ITEM_UNSELECT()
        For Each A As ListViewItem In Me.Items
            A.Checked = False
        Next
    End Sub

    ''' <summary>
    ''' 선택 반전
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub ITEM_DESELECT()
        For Each A As ListViewItem In Me.Items
            A.Checked = IIf(A.Checked, False, True)
        Next
    End Sub

    ''' <summary>
    ''' 선택삭제
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub ITEM_SELECTDEL()
        For Each A As ListViewItem In Me.Items
            If A.Checked Then A.Remove()
        Next
    End Sub





    ''' <summary>
    ''' 리스트뷰 각열이 크기를 확인합니다.
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub COLSIZE_SAVE()
        Dim a As New MyINI2(My.Application.Info.DirectoryPath & "\LISTVIEW.INI")
        For Each COL As ColumnHeader In Me.Columns
            a.Write(Me.Parent.Name & "-" & Me.Name, "COLSIZE" & COL.Index.ToString, COL.Width)
        Next
    End Sub

    ''' <summary>
    ''' 리스트뷰 각열의 크기를 파일로부터 불러옵니다.
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub COLSIZE_LOAD()
        Dim FILEname As String = My.Application.Info.DirectoryPath & "\LISTVIEW.INI"
        Dim a As New MyINI2(My.Application.Info.DirectoryPath & "\LISTVIEW.INI")
        If Not File.Exists(FILEname) Then
            MsgBox("열너비 파일이 존재하지 않습니다", MsgBoxStyle.Critical, "열너비설정-오류")
            COLSIZE_SAVE()
            Return
        End If
        'MsgBox("PARENT" & Me.Parent.Name.ToString & "/" & Me.Parent.Text)

        For Each COL As ColumnHeader In Me.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, "COLSIZE" & COL.Index.ToString)
            Catch ex As Exception   '//없을경우에는 에러를 내므로 그냥 나간다

            End Try
        Next
    End Sub

    Public Sub FileSave()

        Dim SD As New SaveFileDialog
        SD.Filter = "탭으로 분리된 파일(*.TXT)|*.TXT|콤마로 분리된 파일(*.CSV)|*.CSV|Excel WorkSheet(*.XLS)|*.XLS|XML Excel WorkSheet|*.xml|HTML(*.HTML)|*.HTML"
        SD.FilterIndex = 1
        SD.RestoreDirectory = True
        If SD.ShowDialog() = DialogResult.OK Then
            Select Case SD.FilterIndex
                Case 1
                    ExportToTXT(SD.FileName)
            End Select
        End If

    End Sub

    Public Function ExportToTXT(ByVal FileName As String, Optional ByVal StartPosition As Short = 2, Optional ByVal NotePadOpen As Boolean = False) As Boolean
        Dim FS As New FileStream(FileName, FileMode.Create)
        'Dim BW As New BinaryWriter(FS, System.Text.Encoding.Default)
        Dim SW As New StreamWriter(FS, System.Text.Encoding.Default)

        Dim rowIndex As Integer = 1 '엑셀의 행값
        Dim Rrowindex As Integer = 1
        Dim colIndex As Integer = 0  '엑셀의 열값
        Dim RcolIndex As Integer = 0


        Windows.Forms.Cursor.Current = Cursors.WaitCursor '//마우스를 대기로 변환

        Try
            Dim LV As ListViewItem
            Dim la As ListViewItem.ListViewSubItem
            Dim SubBuf As StringBuilder
            Dim Aa As ColumnHeader

            '--열머리글 내보내기
            For Each Aa In Me.Columns
                'MsgBox(Aa.Text)
                colIndex += 1
                If colIndex >= StartPosition Then
                    RcolIndex += 1
                    SW.Write(Aa.Text & vbTab)
                    'excelApp.Cells(1, RcolIndex) = Aa.Text
                End If
            Next
            SW.Write(vbCrLf)

            '- 열데이터 내보내기
            For Each LV In Me.Items
                rowIndex += 1   '//행값을 1개 증가시킨다.
                colIndex = 0    '//열값은 초기화한다.
                RcolIndex = 0    '//열값은 초기화한다.
                SubBuf = New StringBuilder  '//문자열 저장버퍼 초기화
                For Each la In LV.SubItems
                    colIndex += 1
                    If colIndex >= StartPosition Then
                        RcolIndex += 1
                        SW.Write(la.Text & vbTab) '//컬럼을 추가한다.
                    End If
                Next
                SW.Write(vbCrLf)
            Next

            SW.Close()
            FS.Close()
            Return True
        Catch exp As Exception
            MessageBox.Show(exp.ToString)
            SW.Close()
            FS.Close()
            Return False
        End Try
    End Function
End Class
