search listbox if string found copy to another listbox?

search listbox if string found copy to another listbox?

Page 1 of 1

7 Replies - 16206 Views - Last Post: 12 March 2012 - 10:44 AM Rate Topic: -----

#1 XMEGA  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 135
  • Joined: 17-November 08

search listbox if string found copy to another listbox?

Post icon  Posted 29 November 2008 - 02:11 PM

I have a listbox which on form load generates correctly from a text file like so in a listbox...ex
"John Doe, 1"
"Bob Brown,2"

I now would like to search the listbox if it contains either a "1" or a "2" which is does in this case and copy the item to another listbox. The 1 and 2 indicate positions I have a position listbox for each position. So 3 listbos, first one contains the names on form load, 2 other empty listboxes one for position 1 other for position 2...

I have tried the following

		If xReservationListBox.SelectedIndex = xReservationListBox.FindString("1") Then
			'MessageBox.Show("Found It")
			xSpot1ListBox.Items.Add(xReservationListBox.Items(0))
		End If

		If xReservationListBox.SelectedIndex = xReservationListBox.FindString("2") Then
			'MessageBox.Show("Found It")
			xSpot2ListBox.Items.Add(xReservationListBox.Items(1))
		End If


^this works but once you switch the names position the wrong name appears its wrong listbox...

This post has been edited by XMEGA: 29 November 2008 - 03:16 PM


Is This A Good Question/Topic? 0
  • +

Replies To: search listbox if string found copy to another listbox?

#2 jacobjordan  Icon User is offline

  • class Me : Perfection
  • member icon

Reputation: 111
  • View blog
  • Posts: 1,498
  • Joined: 11-June 08

Re: search listbox if string found copy to another listbox?

Posted 29 November 2008 - 05:30 PM

I'm not sure i quite have all the pieces of the puzzle here, but it seems to me if you simply change it so it looks like this
        If xReservationListBox.SelectedIndex = xReservationListBox.FindString("1") Then
            'MessageBox.Show("Found It")
            'I changed up the next line
            xSpot1ListBox.Items.Add(xReservationListBox.SelectedItem)
        End If

        If xReservationListBox.SelectedIndex = xReservationListBox.FindString("2") Then
            'MessageBox.Show("Found It")
            'I also changed up the next line
            xSpot2ListBox.Items.Add(xReservationListBox.SelectedItem)
        End If


that should work.
Was This Post Helpful? 0
  • +
  • -

#3 XMEGA  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 135
  • Joined: 17-November 08

Re: search listbox if string found copy to another listbox?

Posted 29 November 2008 - 05:55 PM

View Postjacobjordan, on 29 Nov, 2008 - 04:30 PM, said:

I'm not sure i quite have all the pieces of the puzzle here, but it seems to me if you simply change it so it looks like this
        If xReservationListBox.SelectedIndex = xReservationListBox.FindString("1") Then
            'MessageBox.Show("Found It")
            'I changed up the next line
            xSpot1ListBox.Items.Add(xReservationListBox.SelectedItem)
        End If

        If xReservationListBox.SelectedIndex = xReservationListBox.FindString("2") Then
            'MessageBox.Show("Found It")
            'I also changed up the next line
            xSpot2ListBox.Items.Add(xReservationListBox.SelectedItem)
        End If


that should work.

I am not exactly selecting/highlighting an item in the listbox I want it too search and if it finds a "1" copy that line/item it was found it too another empty listbox...

main listbox has the following
item0 say "Bob,1"
item1 say "Jason,2
item2 say "Mike,2"

listbox for spot 1 has nothing, now if it finds the 1 which it will copy that line to the empty listbox so...
item0 should be "Bob,1"

the same thing should happen for listbox for spot 2 and spot 3...fill in the data if # match found

right now I have this method working
		If xReservationListBox.SelectedIndex = xReservationListBox.FindString("1") Then
			xSpot1ListBox.Items.Clear()
			xSpot1ListBox.Items.Add(xReservationListBox.Items(0))
		End If

		If xReservationListBox.SelectedIndex = xReservationListBox.FindString("2") Then
			xSpot2ListBox.Items.Clear()
			xSpot2ListBox.Items.Add(xReservationListBox.Items(1))
		End If


Was This Post Helpful? 0
  • +
  • -

#4 jacobjordan  Icon User is offline

  • class Me : Perfection
  • member icon

Reputation: 111
  • View blog
  • Posts: 1,498
  • Joined: 11-June 08

Re: search listbox if string found copy to another listbox?

Posted 29 November 2008 - 06:15 PM

I am really confused right now. You say your listbox might look like

Bob,1
Jason,2
Mike,2

and you are using a FindString method to find the items that have a 1 or a 2, but a FindString will only pick out the items that start with the inputted string, so it should return -1 because it didn't find any matches when you searched "1" or "2".

If i have this right, you want to search all the items in the list box and add them to xSpot2ListBox if they are marked with a "1" or a "2" at the end. This should do that:
For Each Itm As String In xReservationListBox.Items
    If Itm.Contains("1") Or Itm.Contains("2") Then
        xSpot2ListBox.Items.Add(Itm)
    End If
Next


Was This Post Helpful? 1
  • +
  • -

#5 XMEGA  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 135
  • Joined: 17-November 08

Re: search listbox if string found copy to another listbox?

Posted 29 November 2008 - 06:28 PM

View Postjacobjordan, on 29 Nov, 2008 - 05:15 PM, said:

I am really confused right now. You say your listbox might look like

Bob,1
Jason,2
Mike,2

and you are using a FindString method to find the items that have a 1 or a 2, but a FindString will only pick out the items that start with the inputted string, so it should return -1 because it didn't find any matches when you searched "1" or "2".

If i have this right, you want to search all the items in the list box and add them to xSpot2ListBox if they are marked with a "1" or a "2" at the end. This should do that:
For Each Itm As String In xReservationListBox.Items
    If Itm.Contains("1") Or Itm.Contains("2") Then
        xSpot2ListBox.Items.Add(Itm)
    End If
Next


:^: you didn't exactly spell out what I wanted but it was enuff to get me in the direction I wanted :)

with your help this does what I wanted if you can improve(repative If's?) that great, but this is a breakthough so far
		For Each Itm As String In xReservationListBox.Items
			If Itm.Contains("1") Then
				xSpot1ListBox.Items.Add(Itm)
			End If

			If Itm.Contains("2") Then
				xSpot2ListBox.Items.Add(Itm)
			End If

			If Itm.Contains("3") Then
				xSpot3ListBox.Items.Add(Itm)
			End If
		Next


Was This Post Helpful? 0
  • +
  • -

#6 jacobjordan  Icon User is offline

  • class Me : Perfection
  • member icon

Reputation: 111
  • View blog
  • Posts: 1,498
  • Joined: 11-June 08

Re: search listbox if string found copy to another listbox?

Posted 29 November 2008 - 06:44 PM

You could compact those If's down into an elseif statement like so
            If Itm.Contains("1") Then
                xSpot1ListBox.Items.Add(Itm)
            ElseIf Itm.Contains("2") Then
                xSpot2ListBox.Items.Add(Itm)
            ElseIf Itm.Contains("3") Then
                xSpot3ListBox.Items.Add(Itm)
            End If


Was This Post Helpful? 0
  • +
  • -

#7 XMEGA  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 135
  • Joined: 17-November 08

Re: search listbox if string found copy to another listbox?

Posted 29 November 2008 - 07:25 PM

View Postjacobjordan, on 29 Nov, 2008 - 05:44 PM, said:

You could compact those If's down into an elseif statement like so
            If Itm.Contains("1") Then
                xSpot1ListBox.Items.Add(Itm)
            ElseIf Itm.Contains("2") Then
                xSpot2ListBox.Items.Add(Itm)
            ElseIf Itm.Contains("3") Then
                xSpot3ListBox.Items.Add(Itm)
            End If


not exactly see the main listbox which holds the main data needs to be looked/searched to repopulate its individual listboxes. ^ will look at 1 find it and then stop without looking if 2 or 3 existed... maybe if I show you another example again

Main Listbox
"Superman, 1"
"Batman, 2"
"Spiderman, 3"

Listbox for Spot 1(ok searchs for "1" sees it exist and copied item to spot1)
"Superman,1"

Listbox for Spot2
"Batman,2"

Listbox for Spot3
"Spiderman,3"

my next step is to create a waiting list which if the spot is full place them in another listbox(waiting listbox) but say the spot opens...So I remove them...fill it in with the top item in the waiting listbox...much thanks for the help thusfar :)
Was This Post Helpful? 0
  • +
  • -

#8 supercookie47@yahoo.co.uk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 12-March 12

Re: search listbox if string found copy to another listbox?

Posted 12 March 2012 - 10:44 AM

I'm not sure of what you are looking for, but maybe this may be useful to you (because i know -from what i've read- that what you are asking is atleast slighlty related to this)
The following code should search your listbox for a match to your query:

 Sub ListMatches(ByVal SearchString As String, ByVal SearchFromListBox As ListBox, _
                       ByRef AddResultsTo As ListBox, Optional ByVal ReplaceSearches As Boolean = True)

	        If ReplaceSearches = True Then AddResultsTo.Items.Clear()
	        For Each _ListItem As String In SearchFromListBox.Items
	            If _ListItem.Contains(SearchString) Then
	                AddResultsTo.Items.Add(_ListItem)
	            End If
	        Next

	    End Sub 



Please bear in mind that I am only 15, this is my first response to a question and that I'm new to VB. (I've tried it and it works, hope it helps.

Attached File(s)


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1