Imports System.ComponentModel
Imports System.Drawing
Imports System.Windows.Forms

'Imports System.Threading

Public Class MySearchBox
    Inherits System.Windows.Forms.TextBox
    Implements INotifyPropertyChanged

    ' Dim sql As New SDL.SQL
    ' Dim KUNDENSQL As New SDL.kundenSQL
    Dim form As Form = Me.FindForm
    'Public KdData As SDL.cKundenFMZOLL
    Public DR As DataRow = Nothing

    Public Property searchActive As Boolean = True
    Public Property _AllowSetValue As Boolean = False
    Public Property _allowFreitext As Boolean = False
    Public Property _hideIfListEmpty As Boolean = True
    Public Property dgvpos As String = "LEFT"
    Public Property _autoSizeGross As Boolean = False

    Public Property SQL_WhereParamList As String()
    Public Property INVISIBLE_COLUMNS As String()

    Public Property SQL_SELECT As String
    Public Property SQL_WHERE As String
    Public Property KEYPARAM As String
    Public Property DISPLAY_PARAM As String
    ' Public Property DISPLAY_PARAM_VALUE As String
    Public Property SQL_ORDER_BY As String
    Public Property conn_art As String = "FMZOLL"
    Public Property key_visible As Boolean = False
    Public Property _allowSpaceAsSplitter As Boolean = False


    Dim SrchHeight As Integer = 300
    Dim SrchWidth As Integer = 300

    Public Event VALUE_CHANGED()

    Dim active = False
    Public Property _value As String
    ' Public Propvalue As String
    ' Public Property _value As String
    '    Get
    '       Return Me.Propvalue
    '   End Get
    '    Set(v As String)
    '   SET_VALUE(v)
    '        OnPropertyChanged("_value")
    '    End Set
    'End Property



    Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged

    Protected Sub OnPropertyChanged(ByVal name As String)
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(name))
    End Sub


    Public WithEvents usrcntl As usrcntlKdSearch '= DirectCast(Me.FindForm.Controls.Find("dgvFindKD", False)(0), DataGridView)

    '    Public WithEvents dgv As DataGridView
    ' Public WithEvents dgvInaktiv As DataGridView

    Dim SQL As New SQL

    Public Sub FireReturn()
        dgvFindKD_Click(Me, New KeyEventArgs(Keys.Return))
    End Sub

    Protected Overrides Sub OnPaint(e As PaintEventArgs)
        Try
            MyBase.OnPaint(e)
        Catch ex As Exception
            Me.Invalidate()
        End Try
    End Sub

    Private Sub KdSearchBox_GotFocus(sender As Object, e As EventArgs) Handles Me.GotFocus
        Try
            Me.SelectionLength = 0
            Me.HideSelection = False
            Me.SelectAll()
            Me.Select()
        Catch ex As Exception
            MsgBox("Es ist ein Fehler beim Such-Feld (" & Me.Name & ") aufgetreten 01: " & vbNewLine & vbNewLine & ex.Message & vbNewLine & vbNewLine & System.Reflection.MethodInfo.GetCurrentMethod.Name & vbNewLine & ex.StackTrace)
        End Try
    End Sub

    Private Sub KdSearchBox_KeyUp(sender As Object, e As KeyEventArgs) Handles Me.KeyUp
        Try
            'Console.WriteLine(String.Format("TEEST 01"))
            If Not searchActive Then Exit Sub

            ' If dgv Is Nothing Then dgv = usrcntl.dgvKundenAktiv
            ' If dgvInaktiv Is Nothing Then dgvInaktiv = usrcntl.dgvKundenInAktiv

            If e.KeyCode = Keys.ShiftKey Or e.KeyCode = Keys.Shift Or e.KeyCode = Keys.Alt Or e.KeyCode = Keys.Control Or e.KeyCode = Keys.Left Or e.KeyCode = Keys.Right Or e.KeyCode = Keys.End Or e.KeyCode = Keys.Home Or e.KeyCode = Keys.CapsLock Then 'Bei Shift Tab..
                If usrcntl IsNot Nothing Then usrcntl.Visible = False
                e.Handled = True
                Exit Sub
            End If


            If e.KeyCode = Keys.Tab Then
                If usrcntl IsNot Nothing Then usrcntl.Visible = False
                e.Handled = True
                Exit Sub
            End If

            If e.KeyCode = Keys.Return Then
                e.Handled = True
                Exit Sub
            End If

            If e.KeyCode = Keys.Up Then
                If usrcntl IsNot Nothing Then prevLKW(usrcntl.dgvKundenAktiv)
                e.Handled = True
                Exit Sub
            End If
            If e.KeyCode = Keys.Down Then
                If usrcntl IsNot Nothing Then nextLKW(usrcntl.dgvKundenAktiv)
                e.Handled = True
                Exit Sub
            End If
            If e.KeyCode = Keys.Escape Then
                hideDgv(usrcntl)
                e.Handled = True
                Exit Sub
            End If


            DR = Nothing
            _value = ""

            If Me.Text.Trim = "" Then
                setMeValue(DR)
                hideDgv(usrcntl)
                Exit Sub
            End If

            If usrcntl Is Nothing Then
                Exit Sub
            End If

            If usrcntl.Visible = False Then
                usrcntl.Width = SrchWidth
                usrcntl.Height = SrchHeight

                Dim locationOnForm As Point = Nothing
                If dgvpos = "LEFT" Or dgvpos = "" Then
                    locationOnForm = Me.FindForm().PointToClient(Me.Parent.PointToScreen(Me.Location))
                ElseIf dgvpos = "RIGHT" Then
                    locationOnForm = Me.FindForm().PointToClient(Me.Parent.PointToScreen(Me.Location))
                    locationOnForm.X = locationOnForm.X - (usrcntl.Width - Me.Width)
                    '  MsgBox(locationOnForm.X & "/" & locationOnForm.Y)
                End If


                If locationOnForm.Y + usrcntl.Height + Me.Height > form.ClientRectangle.Height Then
                    usrcntl.Height = form.ClientRectangle.Height - locationOnForm.Y - Me.Height
                End If

                usrcntl.Location = locationOnForm
                usrcntl.Top += Me.Height
            End If

            With usrcntl.dgvKundenAktiv

                If usrcntl.Visible = False Then
                    .AllowUserToAddRows = False
                    .AllowUserToDeleteRows = False
                    .AllowUserToOrderColumns = False
                    .AllowUserToResizeColumns = False
                    .AllowUserToResizeRows = False
                    .ReadOnly = True
                    .MultiSelect = False
                    .SelectionMode = DataGridViewSelectionMode.FullRowSelect
                    .BackgroundColor = Color.White
                    .ForeColor = Color.Black
                    .GridColor = Color.White

                End If

                    'THREADING AKTIV
                    Me.ThreadInitAKTIV()
                    ' Dim ThreadInitAKTIV = New System.Threading.Thread(AddressOf Me.ThreadInitAKTIV)
                    ' ThreadInitAKTIV.IsBackground = True
                    ' ThreadInitAKTIV.Start()

            End With


            usrcntl.dgvKundenInAktiv.Visible = False
            usrcntl.Panel1.Visible = False


            If _hideIfListEmpty Then


                If usrcntl.dgvKundenAktiv.RowCount = 0 And usrcntl.dgvKundenInAktiv.RowCount = 0 Then
                    setObjectVisible(usrcntl, False)
                Else
                    setObjectVisible(usrcntl, True)
                End If
                If False Then

                    'THREADING ThreadWaitAndSee
                    Dim ThreadWaitAndSee = New System.Threading.Thread(Sub()
                                                                           Threading.Thread.Sleep(200)
                                                                           If usrcntl.dgvKundenAktiv.RowCount = 0 And usrcntl.dgvKundenInAktiv.RowCount = 0 Then
                                                                               setObjectVisible(usrcntl, False)
                                                                           Else
                                                                               setObjectVisible(usrcntl, True)
                                                                           End If
                                                                       End Sub)
                    ThreadWaitAndSee.IsBackground = True
                    ThreadWaitAndSee.Start()
                End If
            Else
                usrcntl.Visible = True
                usrcntl.Show()
                usrcntl.BringToFront()
            End If

        Catch ex As Exception
            MsgBox("Es ist ein Fehler beim Such-Feld (" & Me.Name & ") aufgetreten 02: " & vbNewLine & vbNewLine & ex.Message & vbNewLine & vbNewLine & System.Reflection.MethodInfo.GetCurrentMethod.Name & vbNewLine & ex.StackTrace)
        End Try
    End Sub

    Public Sub hideDgv(o) '(sender As Object, e As EventArgs)
        If o IsNot Nothing Then
            Me.searchActive = False
            o.visible = False
            Me.searchActive = True
        End If
    End Sub


    Sub ThreadInitAKTIV()
        Try
            If usrcntl Is Nothing Then Exit Sub
            If usrcntl.dgvKundenAktiv Is Nothing Then Exit Sub
            With usrcntl.dgvKundenAktiv

                Dim srch As String = Me.Text.Replace("'", "").ToString.Trim
                If _allowSpaceAsSplitter Then srch = srch.Replace(" ", ",")

                Dim srch2 As String = ""
                If srch.Contains(",") Then
                    Dim spitter() = srch.Split(",")
                    srch = spitter(0).ToString.Trim
                    srch2 = spitter(1).ToString.Trim
                End If
                Dim topAnz = 16
                Dim AvisoEmail = ""
                Dim SQLstr As String = "SELECT TOP " & topAnz & " " & SQL_SELECT & " WHERE 1=1 "
                SQLstr &= If(SQL_WHERE <> "", " AND " & SQL_WHERE, "")

                If srch <> "" And SQL_WhereParamList.Count > 0 Then 'erster Suchparameter
                    SQLstr &= " AND ( 1<>1 "
                    For Each s In SQL_WhereParamList
                        SQLstr &= " OR " & s & " LIKE '" & srch & "%' "
                    Next
                    SQLstr &= " ) "
                End If

                If srch2 <> "" And SQL_WhereParamList.Count > 0 Then 'Wenn zweiter Suchparameter
                    SQLstr &= " AND ( 1<>1 "
                    For Each s In SQL_WhereParamList
                        SQLstr &= " OR " & s & " LIKE '" & srch2 & "%' "
                    Next
                    SQLstr &= " ) "
                End If

                If SQL_ORDER_BY <> "" Then SQLstr &= " ORDER BY " & SQL_ORDER_BY
                'MsgBox(SQLstr)
                ' MsgBox(SQL.loadDgvBySql(SQLstr, conn_art).Rows.Count)
                setDS(usrcntl.dgvKundenAktiv, SQL.loadDgvBySql(SQLstr, conn_art))

            End With

        Catch ex As Exception
            MsgBox("Es ist ein Fehler beim Such-Feld (" & Me.Name & ") aufgetreten 03: " & vbNewLine & vbNewLine & ex.Message & vbNewLine & vbNewLine & System.Reflection.MethodInfo.GetCurrentMethod.Name & vbNewLine & ex.StackTrace)
        End Try
    End Sub


    Public Sub SET_VALUE(KEYPARAM_Value, Optional showerror = True)
        Try

            If Not active Then Exit Sub
        If KEYPARAM_Value Is Nothing Then KEYPARAM_Value = ""
        Dim SQLstr As String = "SELECT TOP 1 " & SQL_SELECT & " WHERE 1=1 "
        SQLstr &= " AND " & KEYPARAM & " = '" & KEYPARAM_Value & "' "
        SQLstr &= If(SQL_WHERE <> "", " AND " & SQL_WHERE, "")
        If SQL_ORDER_BY <> "" Then SQLstr &= " ORDER BY " & SQL_ORDER_BY
            '  MsgBox(SQLstr)
            Dim dttmp As DataTable = SQL.loadDgvBySql(SQLstr, conn_art, , showerror)
            If dttmp IsNot Nothing AndAlso dttmp.Rows.Count > 0 Then
                setMeValue(dttmp.Rows(0))
            Else
                If _allowFreitext Then
                    Me.Text = KEYPARAM_Value
                    Me._value = KEYPARAM_Value
                Else
                    Me.Text = ""
                    Me._value = ""
                End If
                hideDgv(usrcntl)
            End If

        Catch ex As Exception
            MsgBox("Es ist ein Fehler beim Such-Feld (" & Me.Name & ") aufgetreten 03: " & vbNewLine & vbNewLine & ex.Message & vbNewLine & vbNewLine & System.Reflection.MethodInfo.GetCurrentMethod.Name & vbNewLine & ex.StackTrace)
        End Try
    End Sub




    Public Function GET_VALUE_OnlyReturn(KEYPARAM_Value, Optional showerror = True)
        Try
            If SQL_SELECT = "" Then Return Nothing 'Ohne dem gehts nicht
            If KEYPARAM = "" Then Return Nothing 'Ohne dem gehts nicht
            If KEYPARAM_Value Is Nothing Then KEYPARAM_Value = ""

            Dim SQLstr As String = "SELECT TOP 1 " & SQL_SELECT & " WHERE 1=1 "
            SQLstr &= " AND " & KEYPARAM & " = '" & KEYPARAM_Value & "' "
            SQLstr &= If(SQL_WHERE <> "", " AND " & SQL_WHERE, "")
            If SQL_ORDER_BY <> "" Then SQLstr &= " ORDER BY " & SQL_ORDER_BY

            Dim dttmp As DataTable = SQL.loadDgvBySql(SQLstr, conn_art, , showerror)
            If dttmp IsNot Nothing AndAlso dttmp.Rows.Count > 0 Then

                Return dttmp.Rows(0)(If(DISPLAY_PARAM = "", KEYPARAM, DISPLAY_PARAM)).ToString
            End If
        Catch ex As Exception
        End Try
        Return Nothing
    End Function


    Public Function GET_VALUE_OnlyReturnBySQL(SQL_str, Optional showerror = True)
        Try
           
            Dim dttmp As DataTable = SQL.loadDgvBySql(SQL_str, conn_art, , showerror)
            If dttmp IsNot Nothing AndAlso dttmp.Rows.Count > 0 Then

                Return dttmp.Rows(0)(If(DISPLAY_PARAM = "", KEYPARAM, DISPLAY_PARAM)).ToString
            End If
        Catch ex As Exception
        End Try
        Return Nothing
    End Function

    'threadsicherer Aufruf
    '   Delegate Sub setLabelCallback(l As DataGridView, t As DataTable)
    Private Sub setDS(l As DataGridView, t As DataTable)
        Try
            If l Is Nothing Then Exit Sub
            If t Is Nothing Then Exit Sub
            ''  If Me.InvokeRequired Then
            'Dim d As New setLabelCallback(AddressOf setDS)
            ' Me.Invoke(d, New Object() {l, t})
            ' Else
            With l
                .Columns.Clear()
                .DataSource = t

                If .ColumnCount = 0 Then Exit Sub

                .RowHeadersVisible = False
                .Columns(KEYPARAM).Visible = key_visible
                .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells
                .ClearSelection()

                If INVISIBLE_COLUMNS IsNot Nothing Then
                    For Each i In INVISIBLE_COLUMNS
                        .Columns(i).Visible = False
                    Next
                End If

            End With
            '  End If

        Catch ex As Exception
            ' Try
            '       MsgBox("Es ist ein Fehler beim Such-Feld (" & Me.name & ") aufgetreten 05: " & vbNewLine & vbNewLine & ex.Message)
            'Catch ex2 As Exception
            '    MsgBox("ERR", vbCritical)
            'End Try
        End Try

    End Sub

    'threadsicherer Aufruf
    'Delegate Sub setObjectVisibleCallback(l As Object, v As Boolean)
    Private Sub setObjectVisible(l As Object, v As Boolean)
        Try
            If l Is Nothing Then Exit Sub
            '    If Me.InvokeRequired Then
            '       Dim d As New setObjectVisibleCallback(AddressOf setObjectVisible)
            'Me.Invoke(d, New Object() {l, v})
            '   Else
            l.visible = v
            If v = True Then
                l.Show()
                l.BringToFront()
            End If
            ' End If
        Catch ex As Exception
            '   MsgBox("Es ist ein Fehler beim Such-Feld (" & Me.name & ") aufgetreten 06: " & vbNewLine & vbNewLine & ex.Message)
        End Try
    End Sub


    Public Sub nextLKW(dgv As DataGridView) '(sender As Object, e As EventArgs)
        Try
            If dgv.SelectedRows.Count > 0 Then
                Dim i As Integer = dgv.SelectedRows(0).Index
                If (i + 1 < dgv.RowCount) Then
                    'dgv.CurrentCell = dgv.Item(2, i + 1)
                    dgv.ClearSelection()
                    dgv.Rows(i + 1).Selected = True
                End If
            Else
                If dgv.Rows.Count > 0 Then ' dgv.CurrentCell = dgv.Item(2, 0)
                    dgv.ClearSelection()
                    dgv.Rows(0).Selected = True
                End If
            End If

        Catch ex As Exception
            MsgBox("Es ist ein Fehler beim Such-Feld (" & Me.Name & ") aufgetreten 03,1: " & vbNewLine & vbNewLine & ex.Message & vbNewLine & vbNewLine & System.Reflection.MethodInfo.GetCurrentMethod.Name & vbNewLine & ex.StackTrace)
        End Try
    End Sub
    Public Sub prevLKW(dgv As DataGridView) '(sender As Object, e As EventArgs)
        Try
            If dgv.SelectedRows.Count > 0 Then
                Dim i As Integer = dgv.SelectedRows(0).Index
                If (i > 0) Then
                    'dgv.CurrentCell = dgv.Item(2, i - 1)
                    dgv.ClearSelection()
                    dgv.Rows(i - 1).Selected = True
                End If
            Else
                If dgv.Rows.Count > 0 Then 'dgv.CurrentCell = dgv.Item(2, dgv.Rows.Count - 1)
                    dgv.ClearSelection()
                    dgv.Rows(dgv.Rows.Count - 1).Selected = True
                End If
            End If
        Catch ex As Exception
            MsgBox("Es ist ein Fehler beim Such-Feld (" & Me.Name & ") aufgetreten 03,2: " & vbNewLine & vbNewLine & ex.Message & vbNewLine & vbNewLine & System.Reflection.MethodInfo.GetCurrentMethod.Name & vbNewLine & ex.StackTrace)
        End Try
    End Sub

    Private Sub usrcntl_CLOSE(sender As Object, e As EventArgs) Handles usrcntl.CLOSE
        hideDgv(usrcntl)
    End Sub


    Private Sub dgvFindKD_Click(sender As Object, e As EventArgs) Handles usrcntl.DGV_Click
        Try
            If usrcntl IsNot Nothing AndAlso usrcntl.dgvKundenAktiv IsNot Nothing Then
                With usrcntl.dgvKundenAktiv
                    If .SelectedRows.Count > 0 Then
                        '  If IsNumeric(.SelectedRows(0).Cells("KdNr").Value) Then
                        setMeValue(ToDataRow(.SelectedRows(0)))
                        hideDgv(usrcntl)
                        'End If
                    End If
                End With
            End If
        Catch ex As Exception
            MsgBox("Es ist ein Fehler beim Such-Feld (" & Me.Name & ") aufgetreten 07: " & vbNewLine & vbNewLine & ex.Message & vbNewLine & vbNewLine & System.Reflection.MethodInfo.GetCurrentMethod.Name & vbNewLine & ex.StackTrace)
        End Try
    End Sub

    Public Shared Function ToDataRow(ByVal Value As DataGridViewRow) As DataRow
        Try
            Dim dv As System.Data.DataRowView = CType(Value.DataBoundItem, DataRowView)
            Return dv.Row
        Catch ex As Exception
            Return Nothing
        End Try
    End Function

    Private Sub TextBox1_PreviewKeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.PreviewKeyDownEventArgs) Handles Me.PreviewKeyDown
        Try
            If usrcntl IsNot Nothing AndAlso usrcntl.dgvKundenAktiv IsNot Nothing Then
                If e.KeyData = Keys.Tab And usrcntl.Visible Then ' wenn usrcntl eingeblendet, soll der TABULATOR als InputKey gesehen werden.
                    If usrcntl.dgvKundenAktiv.SelectedRows.Count > 0 Then e.IsInputKey = True Else usrcntl.Visible = False
                End If
            End If
        Catch ex As Exception
            MsgBox("Es ist ein Fehler beim Such-Feld (" & Me.Name & ") aufgetreten 09: " & vbNewLine & vbNewLine & ex.Message & vbNewLine & vbNewLine & System.Reflection.MethodInfo.GetCurrentMethod.Name & vbNewLine & ex.StackTrace)
        End Try
    End Sub
    Private Sub dgvFindKD_Click(sender As Object, e As KeyEventArgs) Handles Me.KeyDown, usrcntl.DGV_KeyDown
        Try
            If usrcntl IsNot Nothing AndAlso usrcntl.dgvKundenAktiv IsNot Nothing Then
                With usrcntl.dgvKundenAktiv
                    If e.KeyCode = Keys.Return Or e.KeyCode = Keys.Enter Or e.KeyCode = Keys.Tab Then
                        If e.KeyCode = Keys.Return And .RowCount > 0 And .SelectedRows.Count = 0 Then .Rows(0).Selected = True

                        If .SelectedRows.Count > 0 Then
                            '      If IsNumeric(.SelectedRows(0).Cells("KdNr").Value) Then
                            setMeValue(ToDataRow(.SelectedRows(0)))
                            hideDgv(usrcntl)
                            'End If
                        Else
                            usrcntl.Visible = False
                        End If
                        e.Handled = True
                        e.SuppressKeyPress = True

                    End If
                End With
            End If
        Catch ex As Exception
            MsgBox("Es ist ein Fehler beim Such-Feld (" & Me.Name & ") aufgetreten 10: " & vbNewLine & vbNewLine & ex.Message & vbNewLine & vbNewLine & System.Reflection.MethodInfo.GetCurrentMethod.Name & vbNewLine & ex.StackTrace)
        End Try
    End Sub


    Private Sub setMeValue(row As DataRow)
        Try
            If Not active Then Exit Sub
            Me.DR = row
            Me._value = ""
            If Me.DR Is Nothing Then
                Me.Text = ""
            Else

                Me.Text = row(If(DISPLAY_PARAM = "", KEYPARAM, DISPLAY_PARAM)).ToString
                Me._value = row(KEYPARAM).ToString
                hideDgv(usrcntl)
            End If
            RaiseEvent VALUE_CHANGED()
            If Me.Text.Length > Me.MaxLength Then Me.Text = Me.Text.Substring(0, Me.MaxLength)
        Catch ex As Exception
            MsgBox("Es ist ein Fehler beim Such-Feld (" & Me.Name & ") aufgetreten 12: " & vbNewLine & vbNewLine & ex.Message & vbNewLine & vbNewLine & System.Reflection.MethodInfo.GetCurrentMethod.Name & vbNewLine & ex.StackTrace)
        End Try
    End Sub




    Private Sub KdSearchBox_PropertyChanged(sender As Object, e As PropertyChangedEventArgs) Handles Me.PropertyChanged
        ' setMeValue() ???????????????
    End Sub

    Sub initSearchBox(control As Control, SQL_SELECT As String, SQL_WhereParamList() As String, SQL_WHERE As String, SQL_ORDER_BY As String, KEYPARAM As String, Optional DISPLAY_PARAM As String = "", Optional conn_art As String = "FMZOLL", Optional key_visible As Boolean = True, Optional SrchWidth As Integer = 300, Optional Srchheight As Integer = 300, Optional INVISIBLE_COLUMNS() As String = Nothing)
        Try
            If control Is Nothing Then Exit Sub
            Me.form = control
            Me.usrcntl = New usrcntlKdSearch
            control.Controls.Add(usrcntl)

            Me.SQL_WhereParamList = SQL_WhereParamList
            Me.SQL_SELECT = SQL_SELECT
            Me.SQL_WHERE = SQL_WHERE
            Me.KEYPARAM = KEYPARAM
            Me.DISPLAY_PARAM = DISPLAY_PARAM
            Me.SQL_ORDER_BY = SQL_ORDER_BY
            Me.conn_art = conn_art
            Me.key_visible = key_visible

            Me.SrchHeight = Srchheight
            Me.SrchWidth = SrchWidth
            Me.INVISIBLE_COLUMNS = INVISIBLE_COLUMNS

            'Me.usrcntl = usrcntl
            hideDgv(Me.usrcntl)
            active = True
        Catch ex As Exception
            MsgBox("Es ist ein Fehler beim Such-Feld (" & Me.Name & ") aufgetreten 13: " & vbNewLine & vbNewLine & ex.Message & vbNewLine & vbNewLine & System.Reflection.MethodInfo.GetCurrentMethod.Name & vbNewLine & ex.StackTrace)
        End Try
    End Sub



    Private Sub KdSearchBox_LostFocus(sender As Object, e As EventArgs) Handles Me.LostFocus
        'THREADING ThreadWaitAndSee
        Dim ThreadWaitAndSee = New System.Threading.Thread(Sub()
                                                               Try
                                                                   Threading.Thread.Sleep(300)
                                                                   ' MsgBox(Me.form.ActiveControl.Name)
                                                                   If Me.form Is Nothing Then Exit Sub
                                                                   If Me.form.ActiveControl Is Nothing Then Exit Sub
                                                                   If Me.form.ActiveControl IsNot Me Then
                                                                       setObjectVisible(usrcntl, False)
                                                                   End If
                                                               Catch ex As Exception

                                                               End Try
                                                           End Sub)
        ThreadWaitAndSee.IsBackground = True
        ThreadWaitAndSee.Start()

        Try
            If usrcntl IsNot Nothing AndAlso usrcntl.dgvKundenAktiv IsNot Nothing AndAlso usrcntl.dgvKundenInAktiv IsNot Nothing Then
                If usrcntl.dgvKundenAktiv.SelectedCells.Count = 0 And usrcntl.dgvKundenInAktiv.SelectedCells.Count = 0 Then
                    Me.SelectionLength = 0
                End If
            End If
        Catch ex As Exception
            MsgBox("KdSearchBox_LostFocus: " & ex.Message)
        End Try
    End Sub



    Private Sub KdSearchBox_Layout(sender As Object, e As LayoutEventArgs) Handles Me.Layout
        If _autoSizeGross Then
            Me.CharacterCasing = Windows.Forms.CharacterCasing.Upper
            ' If Me.Text <> "" Then Me.Text = Me.Text.ToUpper
        End If
    End Sub

    Private Sub KdSearchBox_TextChanged(sender As Object, e As EventArgs) Handles Me.TextChanged
        If Me.Text.Length > Me.MaxLength Then Me.Text = Me.Text.Substring(0, Me.MaxLength)
    End Sub



    Private Sub MySearchBox_Leave(sender As Object, e As EventArgs) Handles Me.Leave
        If Me._value = "" And Me.Text <> "" Then
            SET_VALUE(Me.Text, False) 'Versucht, ob der Wert genau passt
        End If
        ' hideDgv(usrcntl) 'Geht ned, sonst kann man auch keinen Mausklick auf die DGV machen!!
    End Sub
End Class
