listbox text

gfleming
01-30-2004, 03:57 PM
Hi there,

Is there a method that will return the position of text in a listbox list.

That is:



listbox.method("Text") 'retuns 6 if "Text" is in the 6th position in the list



Any help is appreciated

VBJoe
01-30-2004, 04:43 PM
Nope. You'll have to code that yourself. Try This:

Public Function SearchListBox(SearchItem As Object) As Integer
Dim objSearch As Object, counter As Integer
For Each s In ListBox1.Items
If objSearch.ToString.ToLower = SearchItem.ToString.ToLower Then
Return counter
Exit Function
End If
counter += 1
Next

Return -1
I whipped this function up pretty fast off the top of my head (without VS.NET), so you might have to make some minor modifications if I screwed anything up (highly likely). If you want to make your search functionality more robust, you might pass back an Object representing the List Box Item that matched the query rather than just the index of the collection item.

gfleming
01-30-2004, 05:01 PM
Thank you for you effort, but I thought there may have already been a suitable "one line" function.

I am presently using



for i = 0 to listbox.UBound - 1
listbox.listindex = i
if listbox.text = "The Text I'm Looking For" then
PositionOfMyText = i
End if
next



Thanks.

Nope. You'll have to code that yourself. Try This:

Public Function SearchListBox(SearchItem As Object) As Integer
Dim objSearch As Object, counter As Integer
For Each s In ListBox1.Items
If objSearch.ToString.ToLower = SearchItem.ToString.ToLower Then
Return counter
Exit Function
End If
counter += 1
Next

Return -1
I whipped this function up pretty fast off the top of my head (without VS.NET), so you might have to make some minor modifications if I screwed anything up (highly likely). If you want to make your search functionality more robust, you might pass back an Object representing the List Box Item that matched the query rather than just the index of the collection item.

Csharp
01-31-2004, 01:58 PM
like this you mean? ...

MessageBox.Show(ListBox1.Items.IndexOf("The Text Your Looking For"))

VBJoe
01-31-2004, 04:13 PM
for i = 0 to listbox.UBound - 1
listbox.listindex = i
If listbox.text = "The Text I'm Looking For" then
PositionOfMyText = i
End if
next


It looks like you're using VB6. The new ListBox control in .NET doesn't have a ListIndex property.

cyrilgupta
01-31-2004, 08:00 PM
There's something called LB_FINDSTRING API. The approaches you've listed are not suitable for textboxes with a large amount of data. What you need to do is to use the SENDMESSAGE function with LB_FINDSTRINg. I don't have any idea right now how this can be done in VB.NET. Maybe you should try looking up a tutorial which teaches you how to use API calls in VB.NET

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum