139 lines
4.8 KiB
VB.net
139 lines
4.8 KiB
VB.net
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)
|
|
If clearList Then MyBase.Items.Clear()
|
|
If firstEmpty Then
|
|
Me.Items.Insert(0, New MyListItem("", ""))
|
|
End If
|
|
For Each i In l : MyBase.Items.Add(i) : Next
|
|
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)
|
|
fillWithMyListItem((New SQL).loadCboBySqlWithListItem(sqlstr, showValueInText, conn), firstEmpty, clearList)
|
|
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 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
|
|
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
|
|
End Class
|
|
|