ComboAutoC




Option Explicit
Dim mblnNoQuickText As Boolean
'Min. of Chars for 'QuickText'

Const htMinChars = 2
Private Sub cboCool_Change()
Dim i As Integer
Dim strText As String
If Len(cboCool.Text) < htMinChars Then Exit Sub
If mblnNoQuickText Then Exit Sub
'Using a variable is better than using a property

strText = cboCool.Text
For i = 0 To cboCool.ListCount
If UCase$(strText) = UCase$(Left$(cboCool.List(i), _
Len(cboCool.Text))) Then
'Found Text in List! Copy 'Text'

cboCool.Text = cboCool.List(i)
'Select: 'QuickText'

cboCool.SelStart = Len(strText)
cboCool.SelLength = Len(cboCool.Text) - Len(strText)
Exit For
End If
Next i
End Sub

Private Sub cboCool_GotFocus()
'Select complete Text

SendKeys String:="{HOME}+{END}", Wait:=True
End Sub

Private Sub cboCool_KeyDown(KeyCode As Integer, Shift As Integer)
Select Case KeyCode
Case vbKeyBack, vbKeyClear, vbKeyDelete
mblnNoQuickText = True
Case vbKeyReturn
mblnNoQuickText = True
Call AddTextToList
Case Else
mblnNoQuickText = False
End Select
End Sub

Private Sub AddTextToList()
Dim n As Integer, m As Integer
Dim strOldData() As String
Dim strText As String
'No 'Text' -> Exit

If cboCool.Text = "" Then Exit Sub
'Copy 'Text'-Property

strText = cboCool.Text
For n = 0 To cboCool.ListCount - 1
'All 'List-Strings' will be copied into an array.

'But if 'Text' is already in 'List',

'the string won't be copied.

'(Otherwise that string would be in 'List' 2x)

If Not cboCool.List(n) = strText Then
m = m + 1
ReDim Preserve strOldData(m)
strOldData(m) = cboCool.List(n)
End If
Next n
cboCool.Clear
'Put the new 'Text' on top

cboCool.AddItem strText
For n = 1 To m
cboCool.AddItem strOldData(n)
Next n
cboCool.SetFocus
End Sub

Private Sub Form_Load()
ScaleMode = vbTwips
Move Left, Top, 6000
cboCool.Move 120, 120, ScaleWidth - 240
cboCool.Text = "I am very cool!"
cmdAdd.Move 120, cboCool.Top + 500, ScaleWidth - 240, 1000
cmdAdd.Caption = "Add 'Text' to 'List'" & vbCrLf & _
"[You can also press 'Return' or 'Enter' to raise _
'cboCool_KeyDown' Event]"

Height = cmdAdd.Top + cmdAdd.Height + 500
End Sub

Put this code into a form. This form needs a Combo (cboCool).
If 'True', do not display that 'QuickText'










( comboautoc.html )- by Paolo Puglisi - Modifica del 17/12/2023