combo box sort

Started by Douglas McDonald, May 20, 2011, 01:17:03 PM

Previous topic - Next topic

Douglas McDonald

I did a search on combo box sort and didn't find anything here. Here's my question / problem. I fill a combo box from a database file adding text and setting the item data. Item data being the rowID (index) of the record. So when I select something from the combo box I can use the item data to do a search in the database. This works great if the combo box is NOT sorted. But if it is sorted the itemdata does not follow the text.

Example:
Say I put in "TO-3" at combobox index 124 and I set Itemdata to 124. If I sort the combobox of coures "TO-3" will no longer be at index 124 but sholdn't the itemdata (124) be associated with the text "TO-3" ?
I want to keep the itemdata associated with the TEXT so I can used it as the ROWID in a DB search

Here I load the bombo box from the database
Sub loadPKG()
Local lResult As Long
Local rec As String

lResult = slSel("SELECT *, rowid FROM package",%rSetPackage,"E")
GetsqlError lResult

Do While slGetRow(%rSetPackage)
   'Incr recn& 'record number
   rStr$ = slFN("RowID",%rSetPackage)
   rec = slFN("name",%rSetPackage)
   
   FF_ComboBox_AddString( HWND_POPUP_PKG, rec)
   FF_ComboBox_SetItemData( HWND_POPUP_PKG, Val(rStr$)-1, Val(rStr$) )
Loop
slCloseSet(%rSetPackage)
End Sub


Here is where I select and item from the combo box, get the itemdata and the search the database

Function POPUP_PKG_CBN_SELCHANGE ( _
                                         ControlIndex  As Long,  _  ' index in Control Array
                                         hWndForm      As Dword, _  ' handle of Form
                                         hWndControl   As Dword, _  ' handle of Control
                                         idComboBox    As Dword  _  ' identifier of combobox
                                         ) As Long

Local index As Long
Local sTemp As String
Local itemData As Long
   index = FF_ComboBox_GetCurSel( HWND_POPUP_PKG )
   sTemp = FF_ComboBox_GetText( HWND_POPUP_PKG, index )
   itemData = FF_ComboBox_GetItemData( HWND_POPUP_PKG, index )
  ? sTemp & Str$(itemData)
   LoadPict itemData
   FF_TextBox_SetText( HWND_ENTRY_TXTENTRY(2), sTemp )
End Function

Sub LoadPict(index As Long) 'load pict from DB based on combobox index
Local lRowID As Long
Local lResult As Long
Local lFileContent As String
Local noStr As String
noStr = ""

   SendMessage HWND_POPUP_PKGIMAGE, %FIREIMAGE_SETIMAGENORMAL, %FIREIMAGE_LOADSTRING, VarPtr(noStr) 'clear picture
   lRowID = index
   ' search the record with RowID=lRowID
   lResult = slSel("SELECT picture FROM package WHERE RowID="+Format$(lRowID), %rSetPackage,"E")
   GetsqlError lResult
   '? Format$(lRowID)
   ' read the record & test if NUL
  If slGetRow(%rSetPackage) Then
   
   lResult = slIsFieldNull (1,%rSetPackage) 'error 14 if field is NUL
   If lResult = %true Then
      'I never get here
      '? "NUL"
   Else
     ' ? "Not Nul"
      ' get the Image in a variable   
      lFileContent = slFN("picture",%rSetPackage)
     'clear loaded picture
     SendMessage HWND_POPUP_PKGIMAGE, %FIREIMAGE_SETIMAGENORMAL, %FIREIMAGE_LOADSTRING, VarPtr(noStr) 
     ' load dynamically a new picture from a string into the control (%FIREIMAGE_SETIMAGENORMAL message with %FIREIMAGE_LOADSTRING)
     SendMessage HWND_POPUP_PKGIMAGE, %FIREIMAGE_SETIMAGENORMAL, %FIREIMAGE_LOADSTRING, VarPtr(lFileContent)           
       
   End If
  End If

  slCloseSet(%rSetPackage)


End Sub

Point is the combo box item data follows the combo box index not the "TEXT" it was put in with. This may be normal but then whats the use of it.

I hope I explained it so you can understand the problem.

Thanks
Doug

BTW I can solve the problem by searching in the combo box text but I shouldn't have to do that



Doug McDonald
KD5NWK
www.redforksoftware.com
Is that 1's and 0's or 0's and 1's?

Roger Garstang

#1
I'm not seeing a sort function, so I'm guessing you mean the automatic sort.  Looking at how you set the item data you hard coded a position though.  When Windows sorts whether it be a listbox, combobox, or listview you have to get the value of where it inserted it so you are manipulating the correct row.  The return value of FF_ComboBox_AddString is what you need.  Set the item data for that index.


FF_ComboBox_SetItemData( HWND_POPUP_PKG, FF_ComboBox_AddString( HWND_POPUP_PKG, rec), Val(rStr$) )

Douglas McDonald

Thanks Roger,

I incorrectly assumed that when sort was selected that item data would  follow the text like if it was a 2 dimensional array: CboxIndex(text,itemdata) then the text and itemdata would be tied together. But it seems it does not work that way.
Doug McDonald
KD5NWK
www.redforksoftware.com
Is that 1's and 0's or 0's and 1's?

Roger Garstang

It does follow the text once tied to the correct one.  What happens is when Sorted it is moved by Windows.  If you have 200 items in a list and insert something starting with an "A" you can't assume it will insert it at position 201.  The return value tells you where Windows put your inserted item once sorted so you can attach your item data to it.  If your 201st item happens to be inserted as the 1st item when sorted all the other items are shifted with their index data linked to them.

When unsorted the function returns a value too of where it put it, but most people just use a loop and assume the index.  The safest way would be to just use the returned value no matter what, then either way the data will get assigned to the correct index.  And, a simple loop just reading values could be used instead of a For loop and any translations between 0 or 1 based indexes to remember when using the For variable.

Douglas McDonald

I understand. Anyway the safest way is to just search the database on the combobox test and then get the index for the record I need. It's just one extra step.

Thanks
Doug McDonald
KD5NWK
www.redforksoftware.com
Is that 1's and 0's or 0's and 1's?

Peter House

Doug,

I recommend you let the control do the work.  I have used this approach for a long time and never had a problem.  I did have an earlier problem where I used your method of searching and after much trouble, realized there were two records with identical text!!!

The RowID is much safer and universally reliable.

Peter

TO-3  Does anybody still use that high power package?  :)