Count Number of Characters

In this article, we will create a program that can count all the characters of a text inputted in a TextBox. Now, let's start this tutorial! 1.Let's start this tutorial by following the following steps in Microsoft Visual Basic 6.0: Open Microsoft Visual Basic 6.0, click Choose Standard EXE, and click Open. 2.Next, add only one Button named Command1 and labeled it as "Count Characters". Add also TextBox named Text1 for our input. You must design your interface like this: design 3.Now put this code for your code module. This code is for Command1_Click:
  1. Private Sub Command1_Click()
  2. Dim i
  3. Chars = 0
  4. For i = 0 To Len(Text1.Text) - 1
  5. Text1.SelStart = i
  6. Text1.SelLength = 1
  7. If Asc(Text1.SelText) > 32 And Asc(Text1.SelText) <= 126 Then
  8. Chars = Chars + 1
  9. End If
  10. Next
  11. MsgBox "The number of CHARACTERS is:" & vbCr & Chars, vbInformation
  12.  
  13. End Sub

Explanation:

We have initialized variable I and Chars which is equal first to 0. Then we have created a For Next Loop which starts at 0 up to the Length string of our TextBox1. We minus it by 1 because it has an index of 0. The SelStart property of Text1 will be equal to 0 or meaning it is empty. The selStart property returns the start content of the string inside the TextBox. And its SelLength Property will be equal to 1. This SelLength Property returns the selected length of our TextBox. The Asc function here returns an integer value that represents the character code corresponding to a character.

Output:

output Download the source code below and try it! :) For more inquiries just contact my number or e-mail below. Best Regards, Engr. Lyndon R. Bermoy IT Instructor/System Developer/Android Developer Mobile: 09102918563 Telephone: 826-9296 E-mail:[email protected] Follow and add me in my Facebook Account: https://www.facebook.com/donzzsky

Add new comment