Best method of selecting a combo item from code

Started by Ian Bayly, December 09, 2009, 12:59:53 AM

Previous topic - Next topic

Ian Bayly

I have a form which contains amongst other items, a few combo controls.
I write the text of the combos to SQLite rather than their index so as to avoid a lot of lookups in other parts of the code.
When I read back from SQLite, and want the stored value to be selected in the combo, I am currently looping through the combo items comparing combo choices with the result from SQLite,  to get the index of the item I want selected.
Want I want, is to present the user with the combo showing as selected, the text I get back from SQLite
There must be a better way - I have spent a few hours with FindString and GetCurSe with no luckl.
Anybody help out a poor old x VB coder!

Ian B

Paul Squires

Hi Ian,

Here is what I would do. I would loop your SQLite table and load the combobox with all of the text choices. After the combobox is loaded, I would do the following search:


   Local sFindText As Long
   Local nIndex As Long

   sFindText = "Index4"  '<-- whatever the user choice is that you need ot find
   
   ' Search for specific text in a combobox
   nIndex = FF_ComboBox_FindStringExact( HWND_FORM1_COMBO1, 0, sFindText )
   
   ' Display/Select the item in the combobox
   FF_ComboBox_SetCurSel HWND_FORM1_COMBO1, nIndex


The FF_ComboBox_FindStringExact function is not sensitive to uppercase or lowercase so you should be able to search using any variation of your find string.

If the above does not work then maybe check to see if the items that you are adding to the combobox may have a trailing space or null character. Maybe try RTrim$(sText, Any Chr$(32,0)) prior to adding the string to the combobox.
Paul Squires
PlanetSquires Software

Paul Squires

You could try looping the combobox but that is pretty old school.  :)


   nIndex = FindComboBoxString( HWND_FORM1_COMBO1, sFindString )
   
   ' Display/Select the item in the combobox
   FF_ComboBox_SetCurSel HWND_FORM1_COMBO1, nIndex


Function FindComboBoxString( ByVal hComboBox As Dword, _
                             ByVal sFindString As String _
                             ) As Long
                             
   Local nNumItems As Long
   Local nFoundAt  As Long 
   Local i         As Long
   
   ' Make the search case insensitive
   sFindString = UCase$(sFindString)
   
   ' Default that we have not found th eitem
   nFoundAt = -1
   
   ' Loop the items
   nNumItems = FF_ComboBox_GetCount( hComboBox )
   
   For i = 0 To nNumItems - 1
      If UCase$(FF_ComboBox_GetText( hComboBox, i )) = sFindString Then
         nFoundAt = i
         Exit For
      End If   
   Next
   
   Function = nFoundAt
   
End Function

Paul Squires
PlanetSquires Software

Ian Bayly

Thanks Paul

Now working fine with your help.

Ian B