Check & Replace text of listbox items

How do I Check if each listbox item contains "$"?

Page 1 of 1

10 Replies - 11254 Views - Last Post: 08 April 2009 - 02:51 PM Rate Topic: -----

#1 Jack Eagles1  Icon User is offline

  • Pugnacious Penguin (inspired by no2pencil)
  • member icon

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

Check & Replace text of listbox items

Post icon  Posted 07 April 2009 - 03:17 PM

Ok First how do I check if each listbox item in a listbox contains an expression (Regardless of how many items there are in the listbox) and secondly how would I replace that with text that I want

So, if the First item in the listbox contained "Http%$$" then how would I change that to "Http://" without affecting the rest of the text in the item

Here is the code I have so far:

For Each item in Listbox1.Items 
Dim Selected_Item As String = ListBox1.SelectedItem
			Dim Slash As String
			Dim pattern As String = "$"
			Slash = System.Text.RegularExpressions.Regex.Replace(Selected_Item, pattern, "/")
			ListBox1.Item = Slash
Next
 



But this doesn't work, I get an error saying "Input cannot be null"

This post has been edited by Jack Eagles1: 07 April 2009 - 03:26 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Check & Replace text of listbox items

#2 mistyfyed  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 13
  • Joined: 18-March 09

Re: Check & Replace text of listbox items

Posted 07 April 2009 - 04:02 PM

You could try something like this

Dim dd As String
		For ee As Integer = 0 To ListBox1.Items.Count - 1
			If ListBox1.Items.Item(ee).ToString.Contains("http%$$") Then
				dd = ListBox1.Items.Item(ee).ToString.Substring(7)
				ListBox1.Items.RemoveAt(ee)
				ListBox1.Items.Insert(ee, "http://" & dd)
			End If
		Next

This post has been edited by mistyfyed: 07 April 2009 - 04:23 PM

Was This Post Helpful? 0
  • +
  • -

#3 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2271
  • View blog
  • Posts: 9,499
  • Joined: 29-May 08

Re: Check & Replace text of listbox items

Posted 07 April 2009 - 04:23 PM

A subroutine for you.
Code it after the all the items have be add to the listbox, pass it a reference to your listbox.
 Public Sub ListboxReplace(ByRef lb As ListBox)
  If lb.Items.Count = 0 Then Exit Sub
  For i As Integer = 0 To lb.Items.Count - 1
   ListBox1.Items(i) = System.Text.RegularExpressions.Regex.Replace(ListBox1.Items(i), "Http%\$\$" , "Http://")
  Next
 End Sub



Edit: Updated the regex

This post has been edited by AdamSpeight2008: 07 April 2009 - 04:39 PM

Was This Post Helpful? 0
  • +
  • -

#4 Jack Eagles1  Icon User is offline

  • Pugnacious Penguin (inspired by no2pencil)
  • member icon

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

Re: Check & Replace text of listbox items

Posted 07 April 2009 - 04:41 PM

TYSM WORKED A DREAM!

TYSM WORKED A DREAM!
Was This Post Helpful? 0
  • +
  • -

#5 Jack Eagles1  Icon User is offline

  • Pugnacious Penguin (inspired by no2pencil)
  • member icon

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

Re: Check & Replace text of listbox items

Posted 08 April 2009 - 12:10 AM

PLS See Below

This post has been edited by Jack Eagles1: 08 April 2009 - 02:54 PM

Was This Post Helpful? 0
  • +
  • -

#6 Jack Eagles1  Icon User is offline

  • Pugnacious Penguin (inspired by no2pencil)
  • member icon

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

Re: Check & Replace text of listbox items

Posted 08 April 2009 - 02:41 AM

PLS See Below

This post has been edited by Jack Eagles1: 08 April 2009 - 02:54 PM

Was This Post Helpful? 0
  • +
  • -

#7 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2271
  • View blog
  • Posts: 9,499
  • Joined: 29-May 08

Re: Check & Replace text of listbox items

Posted 08 April 2009 - 07:10 AM

Your regex is wrong $ on its own has a special meaning (End of string/line).
Was This Post Helpful? 0
  • +
  • -

#8 Jack Eagles1  Icon User is offline

  • Pugnacious Penguin (inspired by no2pencil)
  • member icon

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

Re: Check & Replace text of listbox items

Posted 08 April 2009 - 08:27 AM

PLS see Below

This post has been edited by Jack Eagles1: 08 April 2009 - 02:53 PM

Was This Post Helpful? 0
  • +
  • -

#9 Jack Eagles1  Icon User is offline

  • Pugnacious Penguin (inspired by no2pencil)
  • member icon

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

Re: Check & Replace text of listbox items

Posted 08 April 2009 - 08:37 AM

PLS See Below

This post has been edited by Jack Eagles1: 08 April 2009 - 02:53 PM

Was This Post Helpful? 0
  • +
  • -

#10 Jack Eagles1  Icon User is offline

  • Pugnacious Penguin (inspired by no2pencil)
  • member icon

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

Re: Check & Replace text of listbox items

Posted 08 April 2009 - 08:51 AM

PLS See Below

This post has been edited by Jack Eagles1: 08 April 2009 - 02:53 PM

Was This Post Helpful? 0
  • +
  • -

#11 Jack Eagles1  Icon User is offline

  • Pugnacious Penguin (inspired by no2pencil)
  • member icon

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

Re: Check & Replace text of listbox items

Posted 08 April 2009 - 02:51 PM

Ok, I now get only 1 type of error:

When I put the code in: http%¥¥www.google.com¥Maps
The output should be: http://www.google.com/Maps
But instead it is: http://www.google.com¥Maps


Here is my code to decode the coded adress:

  Dim TextboxText As String
	Dim info As IO.FileInfo
  Try
			Dim SelectedLstItem As String
			If first = 0 Then
				first = first + 1
			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 = System.Text.RegularExpressions.Regex.Replace(SelectedLstItem, pattern, "/")
				SelectedLstItem = Slash
			End If
			SelectedLstItem = ListBox1.SelectedItem.ToString
			If SelectedLstItem.Contains("¥¥") Then
				Dim Slash As String
				Dim pattern As String = "¥¥"
				Slash = System.Text.RegularExpressions.Regex.Replace(SelectedLstItem, pattern, "//")
				SelectedLstItem = Slash
			End If
			If SelectedLstItem.Contains("%") Then
				Dim Slash As String
				Dim pattern As String = "%"
				Slash = System.Text.RegularExpressions.Regex.Replace(SelectedLstItem, pattern, ":")
				SelectedLstItem = Slash
			End If
			ListBox1.Items.Remove(ListBox1.SelectedItem)
			ListBox1.Items.Add(SelectedLstItem)
		Catch ex As Exception




Any Ideas?

This post has been edited by Jack Eagles1: 08 April 2009 - 02:52 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1