How To Make A Lyrics Fetcher In VB.NET

Description of the program : You can get the lyrics of songs with the artist name & the song name from this program.

 As usual just create a new fresh project and add the following tools to your form

  • 2 Buttons
  • 2 TextBoxes
  • 1 RichTextBox
  • 1 SaveFileDialog
Usage of the tools that you have added(Don't forget to change the buttons text : 
  1. Button1 - Fetch
  2. Button2 - Save
  3. TextBox1 - Artist Name
  4. TextBox2 - Song Name
  5. RichTextBox1 - Lyrics
You have to add System.Web reference, select add reference from project menu then search for it and click ok :

Adding reference to VB.NET
Now arrange your tools as you see in the following picture(add two labels for the TextBoxes)
Lyrics Fetcher programming
First add these namespaces :
Imports System.Net
Imports System.Web.HttpUtility

Now coming to the main parts, we are using a function to parse the lyrics so first add this function to your codes
  Public Function ParseBetween(ByVal Html As String, ByVal Before As String, ByVal After As String, ByVal Offset As Integer) As String
        If String.IsNullOrEmpty(Html) Then Return String.Empty
        If Html.Contains(Before) Then
            Dim Result As String = Html.Substring(Html.IndexOf(Before) + Offset)
            If Result.Contains(After) AndAlso Not String.IsNullOrEmpty(After) Then Result = Result.Substring(0, Result.IndexOf(After))
            Return Result
        Else
            Return String.Empty
        End If
    End Function

Next double click the Button1(Button fetch) and add the following codes :

   If Not String.IsNullOrWhiteSpace(Textbox1.Text) AndAlso Not String.IsNullOrWhiteSpace(Textbox2.Text) Then
            Try
                Dim src As String = New WebClient().DownloadString(New Uri(String.Format("http://www.songlyrics.com/{0}/{1}-lyrics/", Textbox1.Text.Replace(" ", "-"), Textbox2.Text.Replace(" ", "-"))))
                Dim elyrics As String = ParseBetween(src, "
", "
", "
".Length).Replace(vbCrLf, "").Replace(" ", vbCrLf) Richtextbox1.Text = HtmlDecode(elyrics).Replace(Environment.NewLine, "") Richtextbox1.SelectAll() Richtextbox1.SelectionAlignment = HorizontalAlignment.Center Catch ex As Exception If ex.Message.Contains("404") Then Richtextbox1.Text = "Lyrics not found" End Try Else MessageBox.Show("Enter song name & artist name to get the lyrics", "Information Missing", MessageBoxButtons.OK, MessageBoxIcon.Asterisk) End If

At last double click your save button and add the following codes to the click event
 'Defining the savefiledialog1 and using the richtextbox plaintext to save the lyrics without messing up the text
        Dim savedialog As New SaveFileDialog
        savedialog.Title = "Save As..."
        savedialog.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"
        If savedialog.ShowDialog = Windows.Forms.DialogResult.OK Then
            richtextBox1.SaveFile(savedialog.FileName, RichTextBoxStreamType.PlainText)
        End If
That's all you just created your own Lyrics Fetcher, here is a working preview :
Lil Wayne > Love Me
I hope you enjoyed making this program :)

16 comments

Mit vb.net 2010 Funktioniert es nicht....

Reply

welche Art von Fehler bekommen Sie?

Reply

Could you please show us the full source code, or post project files somewhere? I have some issues (mostly System.ArgumentExceptions).

Reply

Hi,

I just updated the button click codes, if you still have the errors please post the error details in the comment :)

Thank you

Reply

It Doesn't work properly

Reply
Nadhmi Prince mod

Hello,
Your code still have many errors please can you check it agin or post us the full project please.
Thanks

Reply

Can you please let me know, what kind of error you get? It works well for me.

Reply
Nadhmi Prince mod

The errors are as the fllowing:

Error 2 String constants must end with a double quote. C:\Users\azer\AppData\Local\Temporary Projects\Lyrics Fetcher\Form1.vb 22 62 Lyrics Fetcher



Error 3 Syntax error. C:\Users\azer\AppData\Local\Temporary Projects\Lyrics Fetcher\Form1.vb 23 1 Lyrics Fetcher



Error 6 'HtmlDecode' is not declared. It may be inaccessible due to its protection level. C:\Users\azer\AppData\Local\Temporary Projects\Lyrics Fetcher\Form1.vb 26 37 Lyrics Fetcher



Error 7 'prselyrics' is not declared. It may be inaccessible due to its protection level. C:\Users\azer\AppData\Local\Temporary Projects\Lyrics Fetcher\Form1.vb 26 48 Lyrics Fetcher


Please help me because I like this project very very very much. Or please publish your project url

Reply

Hi,

I'm very sorry, it had small mistakes now I have solved them. Please try it again from the beginning :)

Thanks

Reply
Nadhmi Prince mod

Hi,
It steel have the same problems (syntax error, HTMLDecode is unknown, also elyrics is unknown) All the problems are about the Button1(Fetch button) code. Please fix it again

Reply

Hi,

I have updated it again.. I'm sure this time it will work :)

Reply
Nadhmi Prince mod

Hi,
Every think is fine, but it steel only one problem:

Error 2 'HtmlDecode' is not declared. It may be inaccessible due to its protection level. C:\Users\azer\Desktop\proh\WindowsApplication1\WindowsApplication1\Form1.vb 21 37 WindowsApplication1

Please help me to fix it.

Reply
Nadhmi Prince mod

Hi,
I fixed that problem but, when I enter the artist name, the song name and click fetch button it search for some time but the reachtextbox1 steel clear and when I enter wrong names it type"Lyrics not found"
Why when I click the fetch button it doesn't show the song lyrics.

Reply

You must enter the very correct artist name and the song name also that song should be available on the site we are using to get the lyrics :)

Reply

I am having problems in this part :

Dim elyrics As String = ParseBetween(src, "
", "
", "
".Length).Replace(vbCrLf, "").Replace("
", vbCrLf)

Not able to write this code properly. Please can you help with this.

Reply

hi my friend have you the sln ??

Reply

Post a Comment

Note: Only a member of this blog may post a comment.