Help with For each and listboxes

I have a listbox and I want to Use For Each Item as Listbox1.Items, bu

Page 1 of 1

2 Replies - 2098 Views - Last Post: 10 April 2009 - 08:47 AM Rate Topic: -----

#1 Jack Eagles1   User is offline

  • Pugnacious Penguin (inspired by no2pencil)
  • member icon

Reputation: 187
  • View blog
  • Posts: 1,152
  • Joined: 10-December 08

Help with For each and listboxes

Post icon  Posted 10 April 2009 - 02:31 AM

Hi, I have this code to replace certain listbox items, but I cannot do this with a For Each statement, as the items in the listbox change during the function of this code. Has anyone got any idea how I could loop this untill all items in the listbox have been checked for the certain charactors specified?


  Try
			Dim SelectedLstItem As String
			If first = 0 Then
				first = first + 1
				ListBox1.SelectedIndex = 0
			Else
				Me.ListBox1.SelectedIndex = Me.ListBox1.SelectedIndex + 1
			End If
			SelectedLstItem = ListBox1.SelectedItem.ToString
			If SelectedLstItem.Contains("") Then
				Dim Slash As String
				Dim pattern As String = ""
				Slash = SelectedLstItem.Replace(pattern, "/")
				SelectedLstItem = Slash
			End If
			If SelectedLstItem.Contains("%") Then
				Dim Slash As String
				Dim pattern As String = "%"
				Slash = SelectedLstItem.Replace(pattern, ":")
				SelectedLstItem = Slash
			End If
			ListBox1.Items.Remove(ListBox1.SelectedItem)
			ListBox1.Items.Add(SelectedLstItem)
		Catch ex As Exception
		End Try




Thanks in advance for any help

Is This A Good Question/Topic? 0
  • +

Replies To: Help with For each and listboxes

#2 microstoc   User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 59
  • Joined: 04-April 09

Re: Help with For each and listboxes

Posted 10 April 2009 - 08:44 AM

View PostJack Eagles1, on 10 Apr, 2009 - 01:31 AM, said:

Hi, I have this code to replace certain listbox items, but I cannot do this with a For Each statement, as the items in the listbox change during the function of this code. Has anyone got any idea how I could loop this untill all items in the listbox have been checked for the certain charactors specified?


  Try
			Dim SelectedLstItem As String
			If first = 0 Then
				first = first + 1
				ListBox1.SelectedIndex = 0
			Else
				Me.ListBox1.SelectedIndex = Me.ListBox1.SelectedIndex + 1
			End If
			SelectedLstItem = ListBox1.SelectedItem.ToString
			If SelectedLstItem.Contains("") Then
				Dim Slash As String
				Dim pattern As String = ""
				Slash = SelectedLstItem.Replace(pattern, "/")
				SelectedLstItem = Slash
			End If
			If SelectedLstItem.Contains("%") Then
				Dim Slash As String
				Dim pattern As String = "%"
				Slash = SelectedLstItem.Replace(pattern, ":")
				SelectedLstItem = Slash
			End If
			ListBox1.Items.Remove(ListBox1.SelectedItem)
			ListBox1.Items.Add(SelectedLstItem)
		Catch ex As Exception
		End Try




Thanks in advance for any help


You are changing the list order by Removing and Adding items to list while you are processing the list. I hate trying to hit moving targets.
You could copy your listbox1 list to a listbox2 and modify the items as you go. Then clear the listbox1 items. Then copy the listbox2 items to listbox1. Then dispose of listbox2.
Even simpler, just modify the Item in the current list. I think this next bit of code should do it ... fingers crossed ...


me.listbox1.items(me.listbox1.selectedindex) = SelectedLstItem



Why not process the listbox items as follows

Dim Pno as integer
For Pno = 1 to me.Listbox1.Items.Count-1
	'select an item
	me.Listbox1.SelectedIndex=Pno
	'test your item value here
	If (me.Listbox1.SelectedItem.ToString = Something) then
		'process and change your item value to a new value here
		Me.ListBox1.Items(Pno) = SomethingElse	
	End If
Next





View Postmicrostoc, on 10 Apr, 2009 - 07:42 AM, said:

View PostJack Eagles1, on 10 Apr, 2009 - 01:31 AM, said:

Hi, I have this code to replace certain listbox items, but I cannot do this with a For Each statement, as the items in the listbox change during the function of this code. Has anyone got any idea how I could loop this untill all items in the listbox have been checked for the certain charactors specified?


  Try
			Dim SelectedLstItem As String
			If first = 0 Then
				first = first + 1
				ListBox1.SelectedIndex = 0
			Else
				Me.ListBox1.SelectedIndex = Me.ListBox1.SelectedIndex + 1
			End If
			SelectedLstItem = ListBox1.SelectedItem.ToString
			If SelectedLstItem.Contains("") Then
				Dim Slash As String
				Dim pattern As String = ""
				Slash = SelectedLstItem.Replace(pattern, "/")
				SelectedLstItem = Slash
			End If
			If SelectedLstItem.Contains("%") Then
				Dim Slash As String
				Dim pattern As String = "%"
				Slash = SelectedLstItem.Replace(pattern, ":")
				SelectedLstItem = Slash
			End If
			ListBox1.Items.Remove(ListBox1.SelectedItem)
			ListBox1.Items.Add(SelectedLstItem)
		Catch ex As Exception
		End Try




Thanks in advance for any help


You are changing the list order by Removing and Adding items to list while you are processing the list. I hate trying to hit moving targets.
You could copy your listbox1 list to a listbox2 and modify the items as you go. Then clear the listbox1 items. Then copy the listbox2 items to listbox1. Then dispose of listbox2.
Even simpler, just modify the Item in the current list. I think this next bit of code should do it ... fingers crossed ...


me.listbox1.items(me.listbox1.selectedindex) = SelectedLstItem



Why not process the listbox items as follows

Dim Pno as integer
For Pno = 1 to me.Listbox1.Items.Count-1
	'select an item
	me.Listbox1.SelectedIndex=Pno
	'test your item value here
	If (me.Listbox1.SelectedItem.ToString = Something) then
		'process and change your item value to a new value here
		Me.ListBox1.Items(Pno) = SomethingElse	
	End If
Next




That should read For Pno = 0 to me.listbox1.items.count-1
Was This Post Helpful? 0
  • +
  • -

#3 Jack Eagles1   User is offline

  • Pugnacious Penguin (inspired by no2pencil)
  • member icon

Reputation: 187
  • View blog
  • Posts: 1,152
  • Joined: 10-December 08

Re: Help with For each and listboxes

Posted 10 April 2009 - 08:47 AM

Ty, that worked :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1