[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1076
  • Last Modified:

Type into Textbox and search Listbox (VB.NET 2003)

Hi Experts,

I have a winform with a textbox and Listbox. I have code (not inc) which loads file names into Listbox (works fine)... the issue is.

I have a file name format which I wish to break down in the textbox search,

FILENAME = e.g.
Request.SoftwareInstall.MS Office 2007.txt

I wish to search for "MS Office 2007"... full-stops must be ignored.

Is this possible? If so, please provide code.

Thanks,
Roberto
Dim index As Integer = Listbox1.FindString(Textbox1)
If index<> -1 Then
Listbox1.SetSelection(index, True)
Listbox1.TopIndex = index
Else
Listbox1.ClearSelected()
End If

Open in new window

0
RobertoFreemano
Asked:
RobertoFreemano
  • 4
  • 3
  • 2
  • +1
1 Solution
 
Jeff CertainCommented:
I see this is tagged as .NET 2003. Is that really a requirement? The LINQ stuff in VS2008/.NET 3.5 would be much, much nicer. (And the generics from VS2005/.NET 2.0 would be nice too)

One way to do this would be something like this:
        If Listbox1.Items.Count > 0 Then
            Dim results(Listbox1.Items.Count - 1)
            Dim itemCount As Integer = 0
            Dim input As String = Textbox1.Text
            For i As Integer = 0 To Listbox1.Items.Count - 1
                If Listbox1.Items(i).ToString.Contains(input) Then
                    results(itemCount) = Listbox1.Items(i).ToString
                    itemCount += 1
                End If
            Next

            ReDim Preserve results(itemCount)
        End If
0
 
Carlos VillegasFull Stack .NET DeveloperCommented:
Hello,try with this:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        SearchListBox(ListBox1, TextBox1.Text)
    End Sub

    Sub SearchListBox(ByVal tb As ListBox, ByVal textToFind As String)
        textToFind = textToFind.ToUpper()
        For i As Integer = 0 To tb.Items.Count - 1
            If tb.Items(i).ToString().ToUpper().Contains(textToFind) Then
                tb.SetSelected(i, True)
                tb.TopIndex = i
                Exit Sub
            End If
        Next
        tb.ClearSelected()
    End Sub

Open in new window

0
 
Mohamed AbowardaSoftware EngineerCommented:
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
RobertoFreemanoAuthor Commented:
Hi Chaosian,

I guess I should think about upgrading my VB.NET 2003 suite... I bought it a long time ago and I know it just works - especially at work.

I have a problem with this line:
If Listbox1.Items(i).ToString.Contains(input) Then

Open in new window

Error - 'Contains is not a member of 'String'

I've tried to find a replacement 'CompareTo' or 'equalTo' with no success :(

yv989c, same error on your line too :(
0
 
RobertoFreemanoAuthor Commented:
Medo3337, this is a great example; however, my winform loads listbox items from a text file ... I cannot adapt the code to detect new text - does this make sense?

I'm sure an experienced programmer would be able to decipher it.

Roberto
0
 
Mohamed AbowardaSoftware EngineerCommented:
How you get the items from the text file?

Normally, you should read the file in string and split the string to array then add the array to the listbox, this way, it won't make any difference by getting listbox items from text file or by adding the listbox items programmatically.
0
 
RobertoFreemanoAuthor Commented:
Then it's my rubbish skills - LOL

I use this code to get the text data:

Dim sourceFolderC As String = "c:\robtools\cat"
        ListBox2.BeginUpdate()
        ListBox2.Items.Clear()
        ListBox2.Items.AddRange((New System.IO.DirectoryInfo(sourceFolderC)).GetFiles("*.txt"))
        ListBox2.EndUpdate()
        Label3.Text = ListBox2.Items.Count & " files"

Open in new window


Then copied this code from your example:

Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        '//gets the total number of items in the first ListBox
        Dim listLength As Integer = (ListBox2.Items.Count - 1)
        '//i -> counter through loops : j -> counter through chars in string
        Dim i, j As Integer
        '//listString -> string of item in ListBox1
        '//newString -> gets added to one char at a time from listString 
        Dim listString, newString As String

        '//self explanitory, lol

        '//loop through all items in ListBox1
        For i = 0 To listLength
            '//one at a time in sequential order
            listString = ListBox2.Items.Item(i)
            '//searches ListBox1 Item for the text in TextBox1
            '//turning all text to lowercase
            If InStr(listString.ToLower, TextBox1.Text.ToLower) Then
                '//if text is found then add ListBox1 Item to ListBox2
                ListBox2.Items.Add(listString)
            End If
        Next
End Sub

Open in new window


and get the following error:

An unhandled exception of type 'System.InvalidCastException' occurred in microsoft.visualbasic.dll
Additional information: Cast from type 'FileInfo' to type 'String' is not valid.

Open in new window

0
 
Carlos VillegasFull Stack .NET DeveloperCommented:
Hi again, then try with this:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        SearchListBox(ListBox1, TextBox1.Text)
    End Sub

    Sub SearchListBox(ByVal tb As ListBox, ByVal textToFind As String)
        textToFind = textToFind.ToUpper()
        For i As Integer = 0 To tb.Items.Count - 1
            If tb.Items(i).ToString().ToUpper().IndexOf(textToFind) > -1 Then
                tb.SetSelected(i, True)
                tb.TopIndex = i
                Exit Sub
            End If
        Next
        tb.ClearSelected()
    End Sub

Open in new window

0
 
Mohamed AbowardaSoftware EngineerCommented:
Use the way I posted above to read the text file.

1. Use TextReader/StringReader to read the text file.
2. Use Split() method to convert the string to array.
3. Use foreach or for next to fill the listbox.

This way you will be able to handle adding each item rather than using AddRange().
0
 
RobertoFreemanoAuthor Commented:
Thanks Guys,
Thanks yv989c ;)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now