9 Replies - 3584 Views - Last Post: 05 May 2013 - 03:17 PM Rate Topic: -----

#1 D@VE  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 04-May 13

Searching a listbox and retrieving & rearranging a string.

Posted 04 May 2013 - 08:43 PM

Hello All,
First off, I'm using VB2010 (ala Visual Studios 2010). I am having trouble with a couple of things. I tried to accomplish these tasks in several diff ways, but will just post what I've tried recently (mostly because I didn't keep track of the different attempted solutions). I have a form with a text box, a list box, and four buttons ("Add", Dlete", Get" and "Search"). The user enters a full name, seperated by a space (first Last), into the text box, the button adds it to the list (formatted in Last, First format via a function), then clears the text box). There is a delete button, which removes the name from the listbox, a "Get" button, which, when a name is selected in the list, returns the name in the original "First Last" format(via another function) and opens a messagebox which displays the name. And there is a "Search" button, which opens an inputbox, request only the last name, then searches the listbox and returns a boolean value to the button event, which opens an input box that either tells the user the name was found, or that it wasn't. I got everything to work, but the search only looks at the first name on the list. I can't figure out which type of loop I should be using, or I'm wording it wrong, because it's not working. The button event works fine, and the part of the function that identifies the last name works fine, it just doesn't increment. This is the code I have for the search function:
  Public Function SearchResults(ByVal blnSearchResult As Boolean) As Boolean

        Dim strNameToSearch As String
        Dim strLastNameToSearch As String
        Dim intCounter As Integer = 0

        lstNames.SelectedIndex = intCounter
        strNameToSearch = lstNames.SelectedItem.ToString

        intIndexOfComma = strNameToSearch.IndexOf(",")
        strLastNameToSearch = strNameToSearch.Substring(0, intIndexOfComma)
        While strLastNameToSearch.ToUpper Like strSearch.ToUpper = False
            Do While intCounter < lstNames.Items.Count
                intCounter += 1
            Loop
        End While
        Return blnSearchResult
    End Function


This is the code that actually returns the true boolean value when the name is first, but I had deleted my incrementing attempts for this one...
Public Function SearchResults(ByVal blnSearch As Boolean) As Boolean

        Dim strNameToSearch As String
        Dim strLastNameToSearch As String

        Dim intCounter As Integer = 0


        ' Works! If lstNames.SelectedItem.ToString.Contains(strSearch) Then
        ' Works! If lstNames.ToString.Contains(strSearch) Then
        '**********************************

        lstNames.SelectedIndex = intCounter
        strNameToSearch = lstNames.SelectedItem.ToString

        ' index the comma in the name.
        intIndexOfComma = strNameToSearch.IndexOf(",")
        strLastNameToSearch = strNameToSearch.Substring(0, intIndexOfComma)
        If strLastNameToSearch.ToUpper Like strSearch.ToUpper Then
            blnSearchResult = True
            If blnSearchResult = False Then
                intCounter += 1
            End If
            '***********************
        End If
        Return blnSearchResult
    End Function



The other problem is that I thought my "Get" button was working fine, but just realized I broke it somewhere down the line, or it was never really working right (which is what I really suspect). I just used the variable from the function that rearranges the names, and it seemed too easy... Here's that code:
 Public Function FirstNameFirst(ByVal strFirstLast As String) As String
        strFirstLast = strFirst & " " & strLast
        Return strFirstLast
    End Function


When i run this, it just returns the last name (in the proper format) that was entered into the list.
A third thing I'm wanting to know (and remember, I'm an extreme noob) is, should I be using an array for this stuff? Would it be easier if I did? Ordinarialy, the teacher tells us what he wants us to use, but he didn't mention anything about an array, and it seems like you should be able to do it easily enough without one, but then again, my app isn't working right, either....


Thanks, D@VE

Sorry, I forgot to take my "Experimenting" code/comments out of the middle chunk of code...

Also.... Let me know if you want the code for the button events (if that will make what I'm trying to do, any clearer)...

Is This A Good Question/Topic? 0
  • +

Replies To: Searching a listbox and retrieving & rearranging a string.

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9048
  • View blog
  • Posts: 33,970
  • Joined: 12-June 08

Re: Searching a listbox and retrieving & rearranging a string.

Posted 04 May 2013 - 08:53 PM

Quote

the button adds it to the list

What sort of list? A list box? A "list<of string>"?

Either way you should be able to traverse the collection, examining if the item in the collection you are currently on 'CONTAINS' the value you are searching for.

Example:
        Dim sSearchWord As String = "Doe"
        Dim foo As New List(Of String) '-- collection
        foo.Add("john doe")
        foo.Add("jane doe")
        foo.Add("bobby droptables")
        foo.Add("frank theTank")

        For i As Int32 = 0 To foo.Count - 1 '-- traverse every item in the collection to the length of the collection
            If foo(i).ToLower.Contains(sSearchWord.ToLower) Then '-- 'contains' the key word
                Console.WriteLine("found it!: " + foo(i))
            End If
        Next




Quote

First off, I'm using VB2010 (ala Visual Studios 2010).

FYI there is on "VB2010".. Visualbasic (the new stuff) is part of the .NET library. That's "dot-net". Sometimes called VB.NET. You reference the .NET framework in it's name.. so if you are using the 3.0 framework, 4.0, 4.5, etc.

Visual Studios is the IDE (integrated development environment) that is the sandbox you make your code in. VisualStudios is not VB.NET, but Visual Studios helps you write VB.NET apps.
Was This Post Helpful? 0
  • +
  • -

#3 D@VE  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 04-May 13

Re: Searching a listbox and retrieving & rearranging a string.

Posted 04 May 2013 - 09:55 PM

Sorry, for several things (I thought I had mentioned that it was a listbox at the beginning of my post) and for what I'm about to say (because I'm probably really going to sound like I don't know what I'm talking about - which I don't). The "Help/about" menu listed the "Product Details" as: Microsoft Visual Basic 2010. On further examination, I see that it also mentions "Microsoft .NET Framework Version 4.0.30319. We just never discussed any of that in my class. My problem is that I'm assuming we should be doing this project using what we've learned in this introductory class, and it's going look strange if I'm coding using what I'm assuming is more advanced code. This makes it more difficult as you have no idea what we have covered in class, but if it helps, I have never heard of "Console.WriteLine". I hope I'm not wasting your time, but I really appreciate your help. and what does "foo" mean?

Also, where in my code would I use your code? What part of my code should be replaced?
Was This Post Helpful? 0
  • +
  • -

#4 D@VE  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 04-May 13

Re: Searching a listbox and retrieving & rearranging a string.

Posted 04 May 2013 - 11:03 PM

I'm guessing "foo" represents my listbox, but I we don't Dim listboxes, so I'm not sure. But, if it is, what is the proper code (in context to my existing code) for line 09? My list box is lstNames, so is that supposed to be:
If lstNames.SelectedIndex(i).ToLower.Contains(sSearchWord.ToLower) Then 


if so, how can you check "contains" on a numeric variable (the index) against a string (sSearch)? If it's supposed to be:
If lstNames.SelectedItem(i).ToLower.Contains(sSearchWord.ToLower) Then 


How can you identify the value(Item) with an integer (i)? I know I'm probably missing something completely, here... I can't press my lack of knowledge enough, I guess.

Also, how would you use the "ToUpper" on an int32 (i)?
Was This Post Helpful? 0
  • +
  • -

#5 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9048
  • View blog
  • Posts: 33,970
  • Joined: 12-June 08

Re: Searching a listbox and retrieving & rearranging a string.

Posted 05 May 2013 - 12:49 AM

View PostD@VE, on 04 May 2013 - 11:55 PM, said:

Sorry, for several things (I thought I had mentioned that it was a listbox at the beginning of my post) and for what I'm about to say (because I'm probably really going to sound like I don't know what I'm talking about - which I don't). The "Help/about" menu listed the "Product Details" as: Microsoft Visual Basic 2010. On further examination, I see that it also mentions "Microsoft .NET Framework Version 4.0.30319. We just never discussed any of that in my class. My problem is that I'm assuming we should be doing this project using what we've learned in this introductory class, and it's going look strange if I'm coding using what I'm assuming is more advanced code.

That's why I told you.

View PostD@VE, on 04 May 2013 - 11:55 PM, said:

This makes it more difficult as you have no idea what we have covered in class, but if it helps, I have never heard of "Console.WriteLine". I hope I'm not wasting your time, but I really appreciate your help.

I should hope you discussed 'console.writeline'.. somewhere...

View PostD@VE, on 04 May 2013 - 11:55 PM, said:

and what does "foo" mean?

Foo is a variable name.. of, in this instance, a "List(Of String) ".

View PostD@VE, on 04 May 2013 - 11:55 PM, said:

Also, where in my code would I use your code? What part of my code should be replaced?

You wouldn't. This is an example of using a collection in a for loop.

[quote name='D@VE' date='05 May 2013 - 01:03 AM' timestamp='1367733790' post='1846944']
I'm guessing "foo" represents my listbox, but I we don't Dim listboxes, so I'm not sure.


View PostD@VE, on 05 May 2013 - 01:03 AM, said:

But, if it is, what is the proper code (in context to my existing code) for line 09? My list box is lstNames, so is that supposed to be:
If lstNames.SelectedIndex(i).ToLower.Contains(sSearchWord.ToLower) Then 


if so, how can you check "contains" on a numeric variable (the index) against a string (sSearch)? If it's supposed to be:
If lstNames.SelectedItem(i).ToLower.Contains(sSearchWord.ToLower) Then 


I am not following.

View PostD@VE, on 05 May 2013 - 01:03 AM, said:

How can you identify the value(Item) with an integer (i)? I know I'm probably missing something completely, here... I can't press my lack of knowledge enough, I guess.

(i) is an index of an array. Collections are just fancy arrays. Arrays are just sequential groupings of similar data.. If you have an array of strings the first index (index (0)) is a string. The second string is (1)... etc.

View PostD@VE, on 05 May 2013 - 01:03 AM, said:

Also, how would you use the "ToUpper" on an int32 (i)?

Do numbers have an upper? Not that I know of.
Was This Post Helpful? 0
  • +
  • -

#6 D@VE  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 04-May 13

Re: Searching a listbox and retrieving & rearranging a string.

Posted 05 May 2013 - 01:20 PM

ok, as far as the 'console.writeline', no we have covered the last chapter that this class )Programming Fundamentals)covers and it hasn't been mentioned. I even checked the index to see if I missed it and it's not there... Sooo... anyway... i know I'm not supposed to put your exact code in, put I was wondering where, in my existing code, would I make the changes as referenced in your example. I rewrote your code to read as:
   Dim sSearchWord As String = "Doe"
    Dim foo As New List(Of String)

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        For i As Int32 = 0 To lstBox1.Items.Count - 1
            If lstBox1.Items(i).ToLower.Contains(sSearchWord.ToLower) Then
                MessageBox.Show("Found it!")
            End If
        Next
    End Sub

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        lstBox1.Items.Add("john doe")
        lstBox1.Items.Add("jane doe")
        lstBox1.Items.Add("bobby droptables")
        lstBox1.Items.Add("frank theTank")
    End Sub
End Class


and it worked very nicely! Buuut, when I tried to use the same idea with my existing code, it did quite mesh. one of the problems is that I am only want a boolean value of "True" returned if the last name matches (If I'm not mistaken, your code looks at the entire contents of the listbox), so the listbox items have to be converted to strings, so I can separate the first and last names (that's why I whittle it down to:
If strLastNameToSearch.ToUpper Like strSearch.ToUpper Then


The code I'm used to using for incrementing the index of the selected listbox entry would look more like (I've added comments to explain what I'm trying to do with that particular chunk/line of code):
Public Function SearchResults(ByVal blnSearch As Boolean) As Boolean

        Dim intCounter As Integer = 0

' sets the initial selected index to o
        lstNames.SelectedIndex = intCounter

' separates the last name and assigns it to a string variable
        strNameToSearch = lstNames.SelectedItem.ToString

' compares it to the "search" string
If strLastNameToSearch.ToUpper Like strSearch.ToUpper Then


         blnSearch = True

'this is supposed to increment the index, but doesn't work
         If blnSearch = False Then
        intCounter += 1
        End If
        End If


Was This Post Helpful? 0
  • +
  • -

#7 D@VE  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 04-May 13

Re: Searching a listbox and retrieving & rearranging a string.

Posted 05 May 2013 - 01:29 PM

Whoops, I left out some code... should look like this:
[code]
Public Function SearchResults(ByVal blnSearch As Boolean) As Boolean

Dim intCounter As Integer = 0

' sets the initial selected index to o
lstNames.SelectedIndex = intCounter

' converts the selected item to a string variable
strNameToSearch = lstNames.SelectedItem.ToString

' separates the last name and assigns it to a string variable
' Indexes the comma in the name.
intIndexOfComma = strNameToSearch.IndexOf(", ")
strLastNameToSearch = strNameToSearch.Substring(0, intIndexOfComma)

' compares it to the "search" string
If strLastNameToSearch.ToUpper Like strSearch.ToUpper Then
blnSearch = True
' if it doesn't match, the index is incremented by one
ElseIf intCounter += 1
End If
Return blnSearch
End Function

Ugh... forgot my closing code tag... sorry...
Was This Post Helpful? 0
  • +
  • -

#8 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3319
  • View blog
  • Posts: 11,223
  • Joined: 12-December 12

Re: Searching a listbox and retrieving & rearranging a string.

Posted 05 May 2013 - 01:32 PM

The line before the test If blnSearch = False sets blnSearch = True, so the test for False will never be met.

Just seen your updated code, and you cannot (or shouldn't) increment a value as part of a conditional:

ElseIf intCounter += 1

and you've omitted Then. Your IDE should have error indicators (wavy lines).

This post has been edited by andrewsw: 05 May 2013 - 01:34 PM

Was This Post Helpful? 0
  • +
  • -

#9 D@VE  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 04-May 13

Re: Searching a listbox and retrieving & rearranging a string.

Posted 05 May 2013 - 02:55 PM

Where should I increment? And I'm really sorry! I keep messing up when I try to type the code in, so I'm going to try just copying and pasting exactly what I have. The lines of asterisks with "Works!" in them are parts of code that are working as intended. The line of asterisks with "WIP 1" in it, is where I tried to implement Modi123_1's solution. I tried to commet as much as I could...
    ' This function receives a string variable, which contains a last name to be used to search the listbox (which is in "Last, First" format).  It returns a boolean value of 
    ' true if the last name is found in the listbox, a value of false if it is not found.  This value is returned.
    Public Function SearchResults(ByVal blnSearch As Boolean) As Boolean

        '***************** Works! ********************************
        ' Declares  varaiable
        Dim intCounter As Integer = 0

        ' sets the initial selected index to o
        lstNames.SelectedIndex = intCounter

        ' Assigns the selected listbox item to a string variable
        strNameToSearch = lstNames.SelectedItem.ToString

        ' Indexes the comma in the name.
        intIndexOfComma = strNameToSearch.IndexOf(", ")

        ' separates the last name and assigns it to a string variable
        strLastNameToSearch = strNameToSearch.Substring(0, intIndexOfComma)

        '***************** WIP 1 ********************************
        ' For i As Int32 = 0 To lstNames.Items.Count - 1
        ' If lstNames.Items.ToString.ToUpper.Contains(strSearch.ToUpper) Then
        ' blnSearch = True
        ' end If
        'Next

        '***************** Works! ********************************
        ' compares it to the "search" string
        If strLastNameToSearch.ToUpper Like strSearch.ToUpper Then
            blnSearch = True

            '*******this is supposed to increment the index, but doesn't work************
        ElseIf blnSearch = False Then
            intCounter += 1
        End If
        '****************** Works! *******************************
        Return blnSearch
    End Function


Was This Post Helpful? 0
  • +
  • -

#10 D@VE  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 04-May 13

Re: Searching a listbox and retrieving & rearranging a string.

Posted 05 May 2013 - 03:17 PM

I also tried:
If strLastNameToSearch.ToUpper Like strSearch.ToUpper Then
        blnSearch = True
        End If
        Do
        intCounter += 1
        Loop Until intCounter > lstNames.Items.Count


and
 While strLastNameToSearch.ToUpper Like strSearch.ToUpper = False
            Do
                intCounter += 1
            Loop Until intCounter > lstNames.Items.Count
        End While
        If strLastNameToSearch.ToUpper Like strSearch.ToUpper = True Then
            blnSearch = True
        End If


the 2nd returned "True" if the name was first in the listbox, but ran (what I'm assuming was) an endless loop for any other name. and yes, I'm trying whatever I can think of...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1