Imports System.ComponentModel

Public Class MyComboBox
    Inherits System.Windows.Forms.ComboBox

    Public Property _allowFreiText As Boolean = False
    Public Property _allowedValuesFreiText As String() = Nothing
    ' Public Property _allowedValuesFreiText_SET As String() = Nothing
   
    Public Sub New()
    End Sub

    Sub fillWithMyListItem(l As List(Of MyListItem), Optional firstEmpty As Boolean = False, Optional clearList As Boolean = True, Optional firstEmptyName As String = "")
        If clearList Then MyBase.Items.Clear()
        If firstEmpty Then
            Me.Items.Insert(0, New MyListItem(firstEmptyName, ""))
        End If
        If l IsNot Nothing Then
            For Each i In l : MyBase.Items.Add(i) : Next
        End If
    End Sub
    Sub fillWithSQL(sqlstr As String, Optional showValueInText As Boolean = True, Optional conn As String = "SDL", Optional firstEmpty As Boolean = False, Optional clearList As Boolean = True, Optional firstEmptyName As String = "")
        fillWithMyListItem((New SQL).loadCboBySqlWithListItem(sqlstr, showValueInText, conn), firstEmpty, clearList, firstEmptyName)
        _TRANSLATE()
    End Sub

    Public Property _value As String
        Get
            Return getValueOfItem()
        End Get
        Set(v As String)

            'If _allowedValuesFreiText Is Nothing OrElse Not valueAllowed(v) Then
            If v = "" Then
                If Me.Items.Count > 0 And Not _allowFreiText Then Me.SelectedItem = Me.Items(0)
            Else
                changeItem(v, valueAllowed(v))
                'End If
            End If
        End Set
    End Property

    Function valueAllowed(t)
        If _allowedValuesFreiText IsNot Nothing Then
            For Each s In _allowedValuesFreiText
                Try : If t.ToUpper = CStr(s).ToUpper Then Return True
                Catch : End Try
            Next
        End If
        Return False
    End Function


    Function changeItem(v, Optional Valueallowed = False) As Boolean
        ' 
        Try

            If v Is Nothing Then Me.Text = "" : Return True
            If v Is DBNull.Value Then Me.Text = "" : Return True
            If Me.Items.Count = 0 Then Me.Text = "" : Return True

            If Me.Items(0).GetType.Name = "MyListItem" Then

                Try : If DirectCast(Me.SelectedItem, MyListItem).Value.ToUpper = CStr(v).ToUpper Then Return True 'warum nochmal?
                Catch : End Try ' Wenn der ausgewählte EIntrag bereits korrekt ist.

                For Each i In Me.Items
                    '    MsgBox(DirectCast(i, MyListItem).Value & " - " & v)
                    If DirectCast(i, MyListItem).Value.ToUpper = CStr(v).ToUpper Then
                        'me.SelectedIndex = i : Return True
                        Me.SelectedItem = i : Return True
                    End If
                Next
                For Each i In Me.Items
                    '    MsgBox(DirectCast(i, MyListItem).Value & " - " & v)
                    If DirectCast(i, MyListItem).Text.ToUpper = CStr(v).ToUpper Then
                        Me.SelectedItem = i : Return True
                    End If
                Next
            Else
                'WEnn die Items normale Strings beinhalten, wird keine  Änderung des ._value Wertes vorgenommen.
                Return True
            End If
            Catch ex As Exception
            '        MsgBox(ex.Message)
        End Try

        If Not _allowFreiText And Not Valueallowed Then
            Me._value = ""
        End If

        If Valueallowed Then
            Me.Text = v
        End If

        Return False
    End Function

    Function getValueOfItem() As String
        If Me.Items.Count = 0 Then Return ""
        Try : Return DirectCast(MyBase.SelectedItem, MyListItem).Value
        Catch
            Try : Return MyBase.SelectedItem.ToString : Catch : End Try
        End Try
        If _allowFreiText Or valueAllowed(Me.Text) Then Return Me.Text
        Return ""
    End Function


    Private Sub MyComboBox_Leave(sender As Object, e As EventArgs) Handles Me.Leave
        If Me.DropDownStyle = Windows.Forms.ComboBoxStyle.DropDown Then
            If Me.Text = "" Then
                If Me.Items.Count > 0 Then Me.SelectedItem = Me.Items(0)
            End If
        End If

        Me._value = Me.Text
        changeItem(CStr(Me.Text), valueAllowed(Me.Text))

        Exit Sub

        If valueAllowed(Me.Text) Then
            Dim t = Me.Text
            changeItem(CStr(t))    'Wenn nicht schon in der Auswahl gefunden, dann wird _value gesetzt
            Me._value = t
        Else
            If Me._value = "" And Me.Text <> "" Then
                If Not changeItem(CStr(Me.Text)) And _allowFreiText Then
                    Me.Text = ""
                    If Me.Items.Count > 0 Then Me.SelectedItem = Me.Items(0)
                    Me._value = ""
                End If
            End If
        End If
        
    End Sub

    Private Sub MyComboBox_SelectedIndexChanged(sender As Object, e As EventArgs) Handles Me.SelectedIndexChanged
        Me._value = getValueOfItem()
        'Geht nicht, da sonst Auswahl nicht Funktioniert

        '  MsgBox(Me._value)
    End Sub


    Public Sub _TRANSLATE()
        Try

            If VERAG_PROG_ALLGEMEIN.cAllgemein._LAN = "" Then Exit Sub
        If VERAG_PROG_ALLGEMEIN.cAllgemein._LAN = "DE" Then Exit Sub

        Dim lanTxtAll = VERAG_PROG_ALLGEMEIN.cAllgemein.TRANSLATE.list.FindAll(Function(x) x.trs_object = Me.FindForm.Name And x.trs_control = Me.Name And x.trs_sprache = VERAG_PROG_ALLGEMEIN.cAllgemein._LAN)
        If lanTxtAll.Count > 0 Then

            Dim listItems(Me.Items.Count) As MyListItem
            Me.Items.CopyTo(listItems, 0)

                Me.Items.Clear()

                For Each li In listItems
                    If li IsNot Nothing Then

                        Dim lanTxt = li.Text
                        For Each txt In lanTxtAll
                            If txt.trs_subControl IsNot DBNull.Value AndAlso txt.trs_subControl = li.Text Then
                                lanTxt = txt.trs_text
                            End If
                        Next
                        '   Dim lanTxt = VERAG_PROG_ALLGEMEIN.cAllgemein.TRANSLATE.list.Find(Function(x) x.trs_object = Me.FindForm.Name And x.trs_control = Me.Name And x.trs_sprache = VERAG_PROG_ALLGEMEIN.cAllgemein._LAN And (x.trs_subControl IsNot DBNull.Value AndAlso x.trs_subControl = l.Text))
                        '  MsgBox(If(lanTxt Is Nothing, "noth " & Me.Name, lanTxt.trs_text))
                        Me.Items.Add(New MyListItem(lanTxt, li.Value))

                    End If
                Next
            End If

        Catch ex As Exception
            MsgBox(ex.Message & ex.StackTrace)
        End Try
    End Sub



End Class

