Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

Creating an Application in VS.net that will Count number of each character.

Author: DevASP
Download Source Code : 645_CountCharacter.zip

This article is about how you can create an application in vs.net that will count the total number of each character present in the text file. Browse the file and open it.

The final output of the application is count of each character and shows you in the rich text box. Here you will fine how to use streams, because streams are used to read the file. How to use the open file dialog to open the file and finally the use of System.IO namespace

  

Steps you will do.

  

Start visual studio and create a new window application.

  

Set the following properties of the form object.

 

 

Name = “frmMain”

FormBorderStyle = “FixedSingle”

MaximizeBox = “False”

Text = “”

TextAlign = “Count Alphabets”

 

 

Place a RichTextBox on the form from toolbox and set its following properties.

 

 

Name = “RTB”

Text = “”

 

Place a command button control on the form and set the following properties.

 

 

Name = “btRead”

Text = “Read File”

 

 

Finally place an open file dialog and set the following properties.

 

 

Name = “OPD”

 

 

That’s all. Your user interface is completed. Now open the code window of form and import the System.IO namespace as below.

 

 

Imports System.IO

 

 

Write the following code in the click event of the btnRead to show the open file dialog to open the file and read the file character by character.

 

 

RTB.Clear()

 

OPD.Filter = "Text files (*.txt)|*.txt"

 

OPD.ShowDialog()

 

Dim filepath As String = OPD.FileName

 

If Not System.IO.File.Exists(filepath) Then

Exit Sub

Else

Dim fileStream As New FileStream(filepath, FileMode.Open, FileAccess.Read)

 

Dim readerStream As New StreamReader(fileStream)

 

Dim letter(25) As Integer

 

Dim strText As String

 

Dim current As Integer

 

Dim intLoopStrText, intLoopLetter As Integer

 

 

Try

strText = readerStream.ReadToEnd

 

            strText = strText.ToUpper

 

            While (intLoopStrText < strText.Length)

 

            current = Asc(strText.Substring(intLoopStrText, 1))

 

                 If current >= 65 And current <= 90 Then

                        letter(current - 65) += 1

                 End If

 

                    intLoopStrText += 1

 

               End While

 

For intLoopLetter = 0 To 25

RTB.Text &= Chr(65 + intLoopLetter) & "=" & letter(intLoopLetter) & vbCrLf

Next

 

readerStream.Close()

 

Catch ex As IOException

           MsgBox(ex.Message)

End Try

 

End If

 

 

Run your application and click the button to open the file. Select the file and click open in the open file dialog. You will see the count of each character in rich text box. Count of each character means that how many times each character appear in the file.

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Creating an Analog & Digital Clock in VB.net.

Disclaimer - Privacy
© 2002-2017 DevASP.net