Posts: 1,006
Threads: 111
Joined: Jul 2010
Reputation:
1
To answer question #1)
[code2=vbnet]Dim itemString As String = ListBox1.Items.Item(0).ToString[/code2]
Please note that the 0 is an item's index.
To answer question #2)
Something like this would do:
[code2=vbnet]Public Function SearchAndGet(ByVal searchTerm As String) As String
'This method will return the closest result from what we have
For Each item In ListBox1.Items
'Scroll through all items in a listbox
Dim itemText As String = item.ToString 'Get item string
If (itemText.Contains(searchTerm)) Then 'Check to see if item has our search var
Return itemText 'Return the item that contains our search var
End If
Next
Return "" 'If we reach here, there were no matches. Return nothing
End Function[/code2]
If you want to create a program where the user can select "Complete match", you will need to implement all methods into one program. Here is a basic example using a TextBox1, Button1, and ListBox1.
[code2=vbnet]Public Class Form1
Public Function SearchAndGet(ByVal searchTerm As String) As String
'This method will return the closest result from what we have
For Each item In ListBox1.Items
'Scroll through all items in a listbox
Dim itemText As String = item.ToString 'Get item string
If (itemText.Contains(searchTerm)) Then 'Check to see if item has our search var
Return itemText 'Return the item that contains our search var
End If
Next
Return "" 'If we reach here, there were no matches. Return nothing
End Function
Private Sub TextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox1.TextChanged
Dim listboxMatch As String = SearchAndGet(TextBox1.Text)
If Not (listboxMatch.Equals("")) Then
ListBox1.SelectedItem = listboxMatch
End If
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
If Not (ListBox1.SelectedItem Is Nothing) Then
TextBox1.Text = ListBox1.SelectedItem.ToString
End If
End Sub
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
ListBox1.Items.AddRange({"apples", "oranges", "keyboards", "books", "awesome", "brandon", "bpforums"})
End Sub
End Class[/code2]
The only problem with this code is that it will select words based on letters within the words, not the order that you type them in. So, we can alter this by adjusting our SearchAndGet Method to the following:
[code2=vbnet]Public Function SearchAndGet(ByVal searchTerm As String) As String
Dim possibleMatches As New List(Of String)
'First, let's check the first letters of everything
If (searchTerm.Length <= 0) Then
Return ""
End If
For Each item In ListBox1.Items
Dim firstLetter As String = item.ToString.Substring(0, 1)
If (firstLetter.Equals(searchTerm.Substring(0, 1))) Then
possibleMatches.Add(item.ToString)
End If
Next
If (possibleMatches.Count <= 0) Then
possibleMatches.AddRange(ListBox1.Items)
End If
'This method will return the closest result from what we have
For Each item In possibleMatches
'Scroll through all items in a listbox
Dim itemText As String = item.ToString 'Get item string
If (itemText.Contains(searchTerm)) Then 'Check to see if item has our search var
Return itemText 'Return the item that contains our search var
End If
Next
Return "" 'If we reach here, there were no matches. Return nothing
End Function[/code2]
Please not, all of these methods were drafted with functionality in mind. They are not meant to be fast or optimized.