Imports IntraSell_DLL

Public Class SpaltenEinstellen

    Private dgView As DataGridView
    Friend WithEvents ListBoxSpalten As CheckedListBox


    Private Sub InitializeComponent()
        Me.ListBoxSpalten = New System.Windows.Forms.CheckedListBox()
        Me.SuspendLayout()
        '
        'ButtonSchliessen
        '
        Me.ButtonSchliessen.Location = New System.Drawing.Point(287, 395)
        '
        'ListBoxSpalten
        '
        Me.ListBoxSpalten.FormattingEnabled = True
        Me.ListBoxSpalten.Location = New System.Drawing.Point(12, 51)
        Me.ListBoxSpalten.Name = "ListBoxSpalten"
        Me.ListBoxSpalten.Size = New System.Drawing.Size(336, 334)
        Me.ListBoxSpalten.TabIndex = 95
        '
        'SpaltenEinstellen
        '
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.ClientSize = New System.Drawing.Size(360, 465)
        Me.Controls.Add(Me.ListBoxSpalten)
        Me.Name = "SpaltenEinstellen"
        Me.Text = "Spalten einstellen"
        Me.Controls.SetChildIndex(Me.ButtonSchliessen, 0)
        Me.Controls.SetChildIndex(Me.ListBoxSpalten, 0)
        Me.ResumeLayout(False)
        Me.PerformLayout()

    End Sub

    Friend Sub KonfiguriereDataGridView(dgView As DataGridView)
        Try
            Me.dgView = dgView
            ListBoxSpalten.Items.Clear()

            For Each col As DataGridViewColumn In dgView.Columns
                ListBoxSpalten.Items.Add(col.Name)
            Next

            Dim VarName = Me.dgView.Name + "_SICHTBARE_SPALTEN"
            If ExistsVarValue(VarName) Then
                Dim checkedStr = VarValue(VarName)
                For i = 0 To checkedStr.Length
                    ListBoxSpalten.SetItemChecked(i, checkedStr(i) = "1")
                Next
            End If
        Catch ex As Exception
            HandleAppError(ex, Silent:=True)
        End Try

        AddHandler ListBoxSpalten.ItemCheck, AddressOf ListBoxSpalten_ItemCheck
    End Sub


    Public Shared Sub KonfiguriereDataGridViewSpalten(dgView As DataGridView)
        Try
            Dim VarName = dgView.Name + "_SICHTBARE_SPALTEN"
            If ExistsVarValue(VarName) Then
                Dim checkedStr = VarValue(VarName)
                For i = 0 To checkedStr.Length
                    If i < dgView.Columns.Count Then
                        dgView.Columns(i).Visible = checkedStr(i) = "1"
                    End If
                Next
            End If

        Catch ex As Exception
            HandleAppError(ex, Silent:=True)
        End Try
    End Sub

    Private Sub ListBoxSpalten_ItemCheck(sender As Object, e As ItemCheckEventArgs)
        Try
            Dim checkedStr = ""
            For Each item In ListBoxSpalten.Items
                If ListBoxSpalten.CheckedItems.Contains(item) Then
                    checkedStr += "1"
                Else
                    checkedStr += "0"
                End If
            Next
            SetVarValue(Me.dgView.Name + "_SICHTBARE_SPALTEN", checkedStr)
            KonfiguriereDataGridViewSpalten(Me.dgView)
        Catch ex As Exception
            HandleAppError(ex, Silent:=True)
        End Try
    End Sub

    Private Sub SpaltenEinstellen_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub

    Private Sub ButtonSchliessen_Click(sender As Object, e As EventArgs) Handles ButtonSchliessen.Click
        KonfiguriereDataGridViewSpalten(Me.dgView)
        Me.Close()
    End Sub
End Class
