Jump to content



Photo

[VB.NET]Listbox string replace

listbox string replace

  • Please log in to reply
16 replies to this topic

#1 xScence

xScence

    Neowinian

  • Joined: 01-January 13

Posted 01 January 2013 - 11:40

First of all Happy New Year Neowins.
The program gets the following string in db.lib NAME > URL
For Example:
GOOGLE > http://google.com
When i start my form loads the whole db.lib file in listbox1 like this:
Posted Image
after that when i double click it will open me http://google.com because i split it with this tag (">")
		Dim arg() As String
		For i As Integer = 0 To ListBox1.Items.Count
			arg = ListBox1.SelectedItem.ToString.Split(">")
		Next
My question is: How can i hide the url addres ("http://") in list box to show only the name Google and when i click it to open http://google.com - Thanks
Btw this program gets the mms: protocol (most used for Online TV) and play it in VLC player.
The whole code:
Imports System.Net
Imports System.IO
Imports System.Web
Public Class Form1
	Dim i As Integer
	Private Sub ДобавиToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ДобавиToolStripMenuItem.Click
		Dim inp As String = InputBox("Линк към програмата", "Декодер", vbOKCancel)
		If inp <> "" Then
			ListBox1.Items.Add(inp)
		End If
	End Sub
	Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
		My.Settings.path = Label1.Text
		Dim w As New System.IO.StreamWriter("db.lib")
		Dim i As Integer
		For i = 0 To ListBox1.Items.Count - 1
			w.WriteLine(ListBox1.Items.Item(i))
		Next
		w.Close()
	End Sub
	Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		Label1.Text = My.Settings.path
		Dim r As New System.IO.StreamReader("db.lib")
		While (r.Peek() > -1)
			ListBox1.Items.Add(r.ReadLine)
		End While
		r.Close()
	End Sub
	Private Sub ListBox1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.DoubleClick
		Dim Client As New WebClient
		Dim arg() As String
		For i As Integer = 0 To ListBox1.Items.Count
			arg = ListBox1.SelectedItem.ToString.Split(">")
		Next
		Try
			Dim html = Client.DownloadString(New Uri(arg(i + 1)))
			Client.Headers.Add(HttpRequestHeader.UserAgent, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)")
			Try
				Dim StartPos As Integer = html.IndexOf("mms")
				Dim EndPos As Integer = html.IndexOf("</object>", StartPos)
				Dim finaloutcome As String = html.Substring(StartPos, EndPos - StartPos)
				Dim Key As String = (finaloutcome.Substring(0, 90))
				Dim Last As String = Key.Replace(""">", "")
				Dim all As String = Last.Replace("<pa", "")
				If Label1.Text = "" Then
					MsgBox("Please coose the folder to your VLC Player!")
				Else
					Shell(Label1.Text & "\vlc.exe " & all)
				End If
			Catch ex As Exception
				MsgBox("This link does not support mms protocol!")
			End Try
		Catch ex As Exception
			MsgBox("I cant connect to the website!")
		End Try
	End Sub
	Private Sub VLCPlayerToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles VLCPlayerToolStripMenuItem.Click
		FolderBrowserDialog1.ShowDialog()
		Label1.Text = FolderBrowserDialog1.SelectedPath
		My.Settings.path = Label1.Text
		My.Settings.Save()
	End Sub
End Class



#2 OP xScence

xScence

    Neowinian

  • Joined: 01-January 13

Posted 02 January 2013 - 04:35

Anyone ?

#3 firey

firey

    F͎̗͉͎͈͑͡ȉ͎̣̐́ṙ͖̺͕͙̓̌è̤̞͉̟̲͇̍̍̾̓ͥͅy͓̍̎̌̏̒

  • Tech Issues Solved: 8
  • Joined: 30-October 05
  • Location: Alberta, Canada
  • OS: Windows 7
  • Phone: Android (4.4.2)

Posted 03 January 2013 - 13:33

You could have an array of all your sites that when clicked in the list it goes to that index and launches the site that's in that index. So what you would end up doing, is dump the db.lib thing into an array (splitting at the > and grabbing the second index) and at the same time put the first split index into the list box.

Then have a Click, or Selected Index Changed event. And just do like:

LaunchSite(siteUrls[lstSites.SelectedIndex]) 'Replacing functions/names accordingly

#4 OP xScence

xScence

    Neowinian

  • Joined: 01-January 13

Posted 03 January 2013 - 21:57

LaunchSite(siteUrls[lstSites.SelectedIndex]) 'Replacing functions/names accordingly

Can i have some examples, thanks.

#5 +Majesticmerc

Majesticmerc

    Resident Idealist

  • Tech Issues Solved: 8
  • Joined: 24-August 05
  • Location: United Kingdom
  • OS: Arch Linux / Win 7
  • Phone: HTC One X

Posted 03 January 2013 - 23:20

I think what Firey means is to create a class member that is an array, and keep your URLs in there separately. So in your class you'd do this (please note, my VB is very rusty, so this will probably not compile, but it should give you an idea as to what to do):

Imports System.Net
Imports System.IO
Imports System.Web
Imports System.Collections.Generic

Public Class Form1
	Private m_urls As List(Of String)

	' We'll use this subroutine to add the site to both the listbox and the list of URLs.
	Private Sub AddSiteToList(ByVal siteString As String)
		Dim stringParts() As String = str.Split(">")

		If stringParts.Length <> 2 Then
			Throw New ArgumentException("Parameter 'siteString' was an unexpected format (value: '" + siteString + "'). Please make sure string is of the format '[Site_Name]>[URL]'")
		End If

		ListBox1.Items.Add(stringParts(0))
		m_urls.Add(stringParts(1))
	End Sub

	' We'll use this function to retrieve the URL for the selected item.
	Private Function GetSelectedUrl() As String
		If ListBox1.SelectedIndex > -1 Then
			GetSelectedUrl = m_urls(ListBox1.SelectedIndex)
		Else
			GetSelectedUrl = ""
		End If
	End Function

	Private Sub ДобавиToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ДобавиToolStripMenuItem.Click
		Dim inp As String = InputBox("Линк към програмата", "Декодер", vbOKCancel)
		If inp <> "" Then
				AddSiteToList(inp)
		End If
	End Sub


	Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
		My.Settings.path = Label1.Text
		Dim w As New System.IO.StreamWriter("db.lib")
		For i As Integer = 0 To ListBox1.Items.Count - 1
			w.WriteLine(ListBox1.Items.Item(i) + ">" + m_urls(i))
		Next
		w.Close()
	End Sub

	Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		Label1.Text = My.Settings.path
		Dim r As New System.IO.StreamReader("db.lib")
		While (r.Peek() > -1)
				AddSiteToList(r.ReadLine)
		End While
		r.Close()
	End Sub

	Private Sub ListBox1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.DoubleClick
		Dim url As String = GetSelectedUrl()

		If url.Length > 0 Then
			Try
				Dim Client As New WebClient
				Dim html = Client.DownloadString(New Uri(url))
				Client.Headers.Add(HttpRequestHeader.UserAgent, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)")
				Try
					Dim StartPos As Integer = html.IndexOf("mms")
					Dim EndPos As Integer = html.IndexOf("", StartPos)
					Dim finaloutcome As String = html.Substring(StartPos, EndPos - StartPos)
					Dim Key As String = (finaloutcome.Substring(0, 90))
					Dim Last As String = Key.Replace(""">", "")
                                        Dim all As String = Last.Replace("<pa", "")
					If Label1.Text = "" Then
						MsgBox("Please coose the folder to your VLC Player!")
					Else
						Shell(Label1.Text & "\vlc.exe " & all)
					End If
				Catch ex As Exception
					MsgBox("This link does not support mms protocol!")
				End Try
			Catch ex As Exception
				MsgBox("I cant connect to the website!")
			End Try
		End If
	End Sub

	Private Sub VLCPlayerToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles VLCPlayerToolStripMenuItem.Click
		FolderBrowserDialog1.ShowDialog()
		Label1.Text = FolderBrowserDialog1.SelectedPath
		My.Settings.path = Label1.Text
		My.Settings.Save()
	End Sub
End Class


#6 firey

firey

    F͎̗͉͎͈͑͡ȉ͎̣̐́ṙ͖̺͕͙̓̌è̤̞͉̟̲͇̍̍̾̓ͥͅy͓̍̎̌̏̒

  • Tech Issues Solved: 8
  • Joined: 30-October 05
  • Location: Alberta, Canada
  • OS: Windows 7
  • Phone: Android (4.4.2)

Posted 04 January 2013 - 01:24

Majestic did pretty much exactly what I was thinking. So yep, hit the nail on the head.

#7 OP xScence

xScence

    Neowinian

  • Joined: 01-January 13

Posted 04 January 2013 - 18:15

Error: SplitString is not declared...

#8 firey

firey

    F͎̗͉͎͈͑͡ȉ͎̣̐́ṙ͖̺͕͙̓̌è̤̞͉̟̲͇̍̍̾̓ͥͅy͓̍̎̌̏̒

  • Tech Issues Solved: 8
  • Joined: 30-October 05
  • Location: Alberta, Canada
  • OS: Windows 7
  • Phone: Android (4.4.2)

Posted 04 January 2013 - 19:31

remove : SplitString=

#9 +Majesticmerc

Majesticmerc

    Resident Idealist

  • Tech Issues Solved: 8
  • Joined: 24-August 05
  • Location: United Kingdom
  • OS: Arch Linux / Win 7
  • Phone: HTC One X

Posted 04 January 2013 - 22:49

I've fixed my code. Sorry about that :)

#10 Kami-

Kami-

    ♫ d(-_-)b ♫

  • Tech Issues Solved: 2
  • Joined: 28-July 08
  • Location: SandBox

Posted 04 January 2013 - 23:27

Personally, I'd have them split on reading from the db.lib file into a key/value pair with the DisplayMember property of the list box set to the key (name of site) and the ValueMember property of list box set to the value (the url) then assign the collection of key/value pairs to the listbox items collection...

Really can't be bothered to write up a sample though...

#11 +Majesticmerc

Majesticmerc

    Resident Idealist

  • Tech Issues Solved: 8
  • Joined: 24-August 05
  • Location: United Kingdom
  • OS: Arch Linux / Win 7
  • Phone: HTC One X

Posted 04 January 2013 - 23:44

Personally, I'd have them split on reading from the db.lib file into a key/value pair with the DisplayMember property of the list box set to the key (name of site) and the ValueMember property of list box set to the value (the url) then assign the collection of key/value pairs to the listbox items collection...

Really can't be bothered to write up a sample though...


That probably would be better, but I picked the easiest solution requiring the fewest changes :p

#12 Kami-

Kami-

    ♫ d(-_-)b ♫

  • Tech Issues Solved: 2
  • Joined: 28-July 08
  • Location: SandBox

Posted 05 January 2013 - 17:41

That probably would be better, but I picked the easiest solution requiring the fewest changes :p

Well, should also note it'd be more ideal to move any non-UI code out of the Form class into it's own class(es) too... so may as well re-architect it a little more anyway... *shrugs*

#13 +Majesticmerc

Majesticmerc

    Resident Idealist

  • Tech Issues Solved: 8
  • Joined: 24-August 05
  • Location: United Kingdom
  • OS: Arch Linux / Win 7
  • Phone: HTC One X

Posted 05 January 2013 - 17:44

Not for the sake of < 100 LoC it wouldn't. KISS.

#14 Kami-

Kami-

    ♫ d(-_-)b ♫

  • Tech Issues Solved: 2
  • Joined: 28-July 08
  • Location: SandBox

Posted 05 January 2013 - 18:11

Not for the sake of < 100 LoC it wouldn't. KISS.

Right, I agree with KISS - however this looks like an educational application (as in one he's working on to learn rather than just a quick fix from someone who knows concepts); in which case I deem it more useful to practice correctly laying out your code as you would for a larger application.

Once people get set in lazy ways they tend to stay in them.

#15 firey

firey

    F͎̗͉͎͈͑͡ȉ͎̣̐́ṙ͖̺͕͙̓̌è̤̞͉̟̲͇̍̍̾̓ͥͅy͓̍̎̌̏̒

  • Tech Issues Solved: 8
  • Joined: 30-October 05
  • Location: Alberta, Canada
  • OS: Windows 7
  • Phone: Android (4.4.2)

Posted 05 January 2013 - 18:19

I agree that in general code should be split out as much as possible (Gui with Gui, logic with logic). Though he doesn't seem to have strong knowledge with the programming, so it would end up just causing more headaches to go and re-do all his code for him. As he won't understand what the program does.