Drop-down Listbox - programatically setting the selected item 
Author Message
 Drop-down Listbox - programatically setting the selected item

Quote:
>that "Air Tankers" was at listindex #4, then it would be easy,
>but I don't

Try: cbName.Text = "Air Tankers"
Bruno


Tue, 03 Apr 2001 03:00:00 GMT  
 Drop-down Listbox - programatically setting the selected item

Quote:
>I tried that... sadly, as it is a listbox (not combo) the text
property is
>read- only.

For listbox:
' declarations
Private Declare Function SendMessageString Lib "user32" Alias
"SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal
wParam As Long, ByVal lParam As String) As Long
Private Const LB_FINDSTRINGEXACT = &H1A2
Private Const LB_ERR = (-1)

' Usage
    Dim stringToFind As String, lRet As Long
    stringToFind = CStr(Text1.Text)
    lRet = SendMessageString(List1.hwnd, _
            LB_FINDSTRINGEXACT, -1, stringToFind)
    List1.ListIndex = lRet

Bruno



Wed, 04 Apr 2001 03:00:00 GMT  
 Drop-down Listbox - programatically setting the selected item

Quote:

> Hi John,

> Yes that will probably work.... but I was hoping to avoid searching like that.
> Some of my listboxes have only a few items (5), but there is one with 300-some
> items.

> I'll keep this in mind if I can't find another way, though.

> (I did something sort of similar in Javascript once using an associative
> array.... can VB do associative arrays??  That might work... that's where,
> instead of saying myArray(4) and it gives you "apple", you can say myArray
> ("apple") and it says 4... or "fruit" or whatever you associated it with.  But
> I don't know how to set one up in VB, or if it is possible.)

If you can get away with adding a field to the SQL database,
you could add a field that would store the index number of
the item selected in the listbox, and write to it when an item
has been selected, so that when you load the record, you would
know exactly which item to display.  If the item index hasn't
been recorded, then resort to a search.  You just wouldn't want
to show this field, or display it in any reports, etc.

If that's not an option, how about creating a one dimensional
array, with elements that range from the starting number of
records to the total number of records, each element correlating
to each record, and each value correlating to the selected list item.
If you know the record number, then you will know what should be
selected in the drop-down listbox via the stored index value.
This does assume that your records are sequentially indexed.
Again, if the info hasn't been recorded, then resort to a search.

Best Regards,
Anonymous VB User



Wed, 04 Apr 2001 03:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Press F1 key while drop-down menu item highlighted, drop-down menu stays on top

2. X-Posted: Can I create a drop down list that will let me select an item

3. Can I create a drop down list that will let me select an item

4. Trouble retrieving selected drop-down item

5. dynamically change a html form when a user select an item from a drop down list

6. automatically scrolling a listbox down to the first selected item

7. Setting ListBox item to selected..

8. Setting the item as selected in listboxes and radio buttons

9. setting selected item in listbox

10. Multiselect listbox: How to SET selected item

11. Programatically setting the SELECTED OPTION of a SELECT

12. How to Drop Down DataCombo List Programatically

 

 
Powered by phpBB® Forum Software