For Programmers: Free Programming Magazines  


Home > Archive > Visual Basic > February 2005 > Listbox









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Listbox
Kenny@msn.com

2005-02-27, 8:55 am

If you know only the text value of an item in a listbox is there a
simple method of determinig its index number?

What happens if it occurs more than once in a listbox howw would you
determine the first index and all indexes

TIA
BT3

2005-02-27, 8:55 am

You could point to the beginning, loop through the entire list to make a
match, then just read your counter.

If it occurrs more than once, just save the counter in an array or
something. If this is what you mean, I'll past some code in.

BT3

<Kenny@msn.com> wrote in message
news:uvu221phsda3kirc3hrseld5c9jee4r9fl@
4ax.com...
> If you know only the text value of an item in a listbox is there a
> simple method of determinig its index number?
>
> What happens if it occurs more than once in a listbox howw would you
> determine the first index and all indexes
>
> TIA



Kenny@msn.com

2005-02-27, 8:55 am

THanks.. Now tthat I got them How do I use the index information to
select them? (Sorry I am new to listboxes)
Kenny@msn.com

2005-02-27, 8:55 am

Just to clarify previous posting

I now how to have the list go to the number

lstLocal.ListIndex = i where i is the index number

But it does not highlight the selection

THanks


On Sun, 27 Feb 2005 02:05:02 -0600, "BT3" <honeypot@epmctc.com> wrote:

>You could point to the beginning, loop through the entire list to make a
>match, then just read your counter.
>
>If it occurrs more than once, just save the counter in an array or
>something. If this is what you mean, I'll past some code in.
>
>BT3
>
><Kenny@msn.com> wrote in message
> news:uvu221phsda3kirc3hrseld5c9jee4r9fl@
4ax.com...
>


J French

2005-02-27, 8:55 am

On Sun, 27 Feb 2005 03:53:51 -0500, Kenny@msn.com wrote:

>THanks.. Now tthat I got them How do I use the index information to
>select them? (Sorry I am new to listboxes)


Option Explicit

Private Sub Command1_Click()
Dim L9&, S$, N&

For L9 = 1 To 10
List1.AddItem "Item" + Str$(L9)
Next

S$ = "Item 8"
For L9 = 0 To List1.ListCount - 1
If List1.List(L9) = S$ Then
N = L9
Exit For
End If
Next

' note: Lists are zero based
MsgBox S$ + " found on index" + Str$(N)

End Sub

Larry Serflaten

2005-02-27, 8:55 am


<Kenny@msn.com> wrote
> THanks.. Now tthat I got them How do I use the index information to
> select them? (Sorry I am new to listboxes)


Check out J French's reply and add this line ahead of the where
the MsgBox pops up:

List1.Selected(N) = True

That will select the N'th item in the list.

LFS


Randy Birch

2005-02-27, 3:55 pm

'paste into form general declarations section ..

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA"
(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As
Any) As Long

Private Const LB_FINDSTRING = &H18F
Private Const LB_FINDSTRINGEXACT = &H1A2


Private Function Listbox_FindItemExact(hwndList As Long, sItem As String,
Optional nFirstItem as long) As Long

Listbox_FindItemExact = SendMessage(hwndList, LB_FINDSTRINGEXACT,
nFirstItem, ByVal sItem)

End Function



'usage syntax ...

nIndex = Listbox_FindItemExact(List1.hwnd, "find this string", -1)

'nIndex now holds either -1 (no match), or the index of the first
matching item in the listbox.


The reason it finds the first item is because of the -1 (from the start)
flag passed to the SendMessge call. In order to find subsequent items, pass
the last-found index:

'first call
nIndex = Listbox_FindItemExact(List1.hwnd, "find this string", -1)

'subsequent calls
if 'nIndex > -1 then
nIndex = Listbox_FindItemExact(List1.hwnd, "find this string",
'nIndex )
endif

Again, -1 will be returned when the search finds no more matches.


--


Randy Birch
MS MVP Visual Basic
http://vbnet.mvps.org/


<Kenny@msn.com> wrote in message
news:uvu221phsda3kirc3hrseld5c9jee4r9fl@
4ax.com...
: If you know only the text value of an item in a listbox is there a
: simple method of determinig its index number?
:
: What happens if it occurs more than once in a listbox howw would you
: determine the first index and all indexes
:
: TIA

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com