Total Pageviews

Friday, September 23, 2016

Create Keyword Density Calculator for SEO in Visual Basic 2015

Visual Basic 2015 Tutorials: Keyword Density Calculator

Visual Basic 2015 is very powerful programming language. You can create almost all type of software using it.
We all know that if you want your website to be high in the search engine searches you need to SEO optimize your content. One of the best and most widely used way for SEO optimization is writing content with proper Keyword density. Proper keyword density should be between 2 and 3%.

Let's create keyword density calculator in Visual Basic 2015.

Keyword density calculator in Visual basic 2015

As you can see on the photo we have several textboxes.

Controls properties:
- .Name = txtKeyword
- .Name = txtWordsNo
- .Name = txtKeywordsNo
- .Name = txtPercent
- .Name = Label5

We have no buttons here because the calculation goes automatically in the time of writing. I added a menu so we can save the written SEO optimized text and open it later.

Visual Basic 2015 Tutorials: Writing the code

First we have to call System.IO

Imports System.IO

    Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
        If txtKeyword.Text <> "" Then
            Static rex As New System.Text.RegularExpressions.Regex("\b", System.Text.RegularExpressions.RegexOptions.Compiled Or System.Text.RegularExpressions.RegexOptions.Multiline)

            txtWordsNo.Text = (rex.Matches(TextBox1.Text).Count / 2).ToString()

            txtKeywordsNo.Text = FindWords(TextBox1.Text, txtKeyword.Text)

            txtPercent.Text = Math.Round(txtKeywordsNo.Text / txtWordsNo.Text * 100, 2)


            If txtPercent.Text > 1.5 And txtPercent.Text <= 3 Then
                Label5.BackColor = Color.Green
            Else
                Label5.BackColor = Color.Red
            End If
        End If
    End Sub

To make the software more useful I added a menu where I can open and save text files from the TextBox control in Visual Basic 2015.

Visual Basic 2015 Tutorials

   Private Sub OpenToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles OpenToolStripMenuItem.Click


        OpenFileDialog1.CheckFileExists = True
        OpenFileDialog1.CheckPathExists = True
        OpenFileDialog1.DefaultExt = "txt"
        OpenFileDialog1.FileName = ""
        OpenFileDialog1.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"
        OpenFileDialog1.Multiselect = False

        If OpenFileDialog1.ShowDialog = DialogResult.OK Then
            Dim oReader As New System.IO.StreamReader(OpenFileDialog1.FileName)
            TextBox1.Text = oReader.ReadToEnd
        End If
    End Sub

    Private Sub SaveToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles SaveToolStripMenuItem.Click

        SaveFileDialog1.DefaultExt = "txt"
        SaveFileDialog1.FileName = ""
        SaveFileDialog1.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"
        SaveFileDialog1.FilterIndex = 2
        SaveFileDialog1.RestoreDirectory = True

        If SaveFileDialog1.ShowDialog() = DialogResult.OK Then
            IO.File.WriteAllText(SaveFileDialog1.FileName, TextBox1.Text)
        End If
    End Sub

We finished the code. This code is perfect and it's not making any mistakes. If you want some changes or you want me to send you Visual Studio files or only as an exe file you can freely contact me in the contact form or by commenting. I will answer with pleasure.
My goal is to make this visual basic 2015 tutorials blog more useful as possible.

No comments:

Post a Comment