[Gambas-user] Auto select item in new combobox

Safiur Rahman isafiur at gmail.com
Sun Feb 5 10:16:18 CET 2023


Hi

I used the following code to auto fill text in Combobox. This worked
well upto gambas 3.17.3.

1) Type any alphabet and the related word is shown in combobox. This
works fine in gambas 3.18.0

2) Press down arrow to open pop up list and then type any alphabet.
The related word is selected. Upto this step gambas 3.18.0 works fine.
But when I press Return/Enter key then selected item is not returned
in gambas 3.18.0 (but worked fine till 3.17.3).

Requesting for improvement in the current code.

Public Sub Form_Open()

  ComboBoxItems.List = ["aaa", "bbb", "ccc", "ddd", "eee", "fff"]

End

Public Sub ComboBoxItems_KeyRelease()

  Dim i As Integer
  Dim textToCursor As String
  Dim textPosition As Integer

  If Key.Code = Key.Down Then
    textPosition = ComboBoxItems.Pos
    textToCursor = Mid(ComboBoxItems.Text, 1, textPosition)
    ComboBoxItems.Popup
    For i = 0 To ComboBoxItems.Count - 1
      If LCase(ComboBoxItems[i].Text) Like LCase(textToCursor) & "*" Then
        ComboBoxItems.Index = i
        Break
      Endif
    Next

  Else If Key.Code = Key.BackSpace Then
    If ComboBoxItems.Pos = 0 Then
    Else
      textPosition = ComboBoxItems.Pos - 1
      textToCursor = Mid(ComboBoxItems.Text, 1, textPosition)
      For i = 0 To ComboBoxItems.Count - 1
        If LCase(ComboBoxItems[i].Text) Like LCase(textToCursor) & "*" Then
          ComboBoxItems.Text = ComboBoxItems[i].Text
          ComboBoxItems.Pos = textPosition
          ComboBoxItems.Select(textPosition, Len(ComboBoxItems.Text) -
textPosition)
          Break
        Endif
      Next
    Endif

  Else
    If ComboBoxItems.Pos = 0 Then
    Else
      textPosition = ComboBoxItems.Pos
      textToCursor = Mid(ComboBoxItems.Text, 1, textPosition)
      For i = 0 To ComboBoxItems.Count - 1
        If LCase(ComboBoxItems[i].Text) Like LCase(textToCursor) & "*" Then
          ComboBoxItems.Text = ComboBoxItems[i].Text
          ComboBoxItems.Pos = textPosition
          ComboBoxItems.Select(textPosition, Len(ComboBoxItems.Text) -
textPosition)
          Break
        Endif
      Next
    Endif

  Endif

End

Public Sub ComboBoxItems_Click()

  TextBox1.Text = ComboBoxItems.Text

End



-- 
Regards
Safiur Rahman
-------------- next part --------------
A non-text attachment was scrubbed...
Name: newcombo.zip
Type: application/zip
Size: 14555 bytes
Desc: not available
URL: <http://lists.gambas-basic.org/pipermail/user/attachments/20230205/a3890c3d/attachment-0001.zip>


More information about the User mailing list