Results 1 to 8 of 8

Thread: Counting Characters in a text box...

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Feb 2003
    Posts
    27

    Counting Characters in a text box...

    I am trying to write an app that will display the number of characters (including spaces) in a text box whose total of characters will be displayed in a label. However, it seems to want to count the Microsoft.VisualBasic.ControlChars.Lf that is generated when I hit enter (or at least I believe that is what is causing the additonal characters).

    I would like to remove the additonal characters so that say if i hit enter five times the number of paragraphs will increase by 5 but the characters amount will stay at 0.

    Thanks

  2. #2
    Frenzied Member MrGTI's Avatar
    Join Date
    Oct 2000
    Location
    Ontario, Canada
    Posts
    1,277

    Wink

    Post the code you already have to count the characters, and we'll see what we can do with it.
    ~Peter


  3. #3

    Thread Starter
    Junior Member
    Join Date
    Feb 2003
    Posts
    27
    Option Strict On
    Option Explicit On
    Imports System.Convert
    Imports Microsoft.VisualBasic.ControlChars
    Imports Course.StringProcessor

    Public Class frmMain
    Inherits System.Windows.Forms.Form

    Private Sub btnAnalyzeText_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAnalyzeText.Click
    ''''''''''''''''''''''''''''''''''''''''''''
    'Variables for counting
    ''''''''''''''''''''''''''''''''''''''''''''
    Dim intCharacterCount As Integer
    Dim intWordCount As Integer = 0
    Dim intSentences As Integer = 0
    Dim intParagraphs As Integer = 0

    Dim WordsTotal As Integer


    Dim intvbL As Integer


    Dim count As Integer

    ''''''''''''''''''''''''''''''''''''''''''''
    'Total Characters in text box
    ''''''''''''''''''''''''''''''''''''''''''''
    If txtTextScan.Text = Nothing Then
    intCharacterCount = 0
    WordsTotal = intCharacterCount - intvbL
    lblCharactersTotals.Text = WordsTotal.ToString

    Else
    intCharacterCount = txtTextScan.TextLength
    lblCharactersTotals.Text = intCharacterCount.ToString
    End If

    ''''''''''''''''''''''''''''''''''''''''''''
    'Total Words in text box
    ''''''''''''''''''''''''''''''''''''''''''''

    Dim strWords() As String

    strWords = Split(txtTextScan.Text, ".")

    For count = 0 To UBound(strWords)
    If txtTextScan.Text = " " Then
    intWordCount = 0
    If Len(strWords(count)) > 0 Then
    intWordCount = intWordCount + 1
    End If
    End If
    Next

    lblWordsTotals.Text = intWordCount.ToString

    ''''''''''''''''''''''''''''''''''''''''''''
    'Total Sentences in text box
    ''''''''''''''''''''''''''''''''''''''''''''

    Dim strLines() As String

    strLines = Split(txtTextScan.Text, ".")

    For count = 1 To UBound(strLines)
    If txtTextScan.Text = " " Then
    intSentences = 0
    Else
    If Len(strLines(count)) > 0 Then
    intSentences = intSentences + 1
    End If
    End If
    Next

    lblSentencesTotals.Text = intSentences.ToString

    ''''''''''''''''''''''''''''''''''''''''''''
    'Total Sentences in text box
    ''''''''''''''''''''''''''''''''''''''''''''

    Dim strParagraphs() As String

    strLines = Split(txtTextScan.Text, Microsoft.VisualBasic.ControlChars.Lf)


    For count = 0 To UBound(strLines)
    If Len(strLines(count)) > 0 Then
    intParagraphs = intParagraphs + 1
    End If
    Next

    lblParagraphsTotals.Text = intParagraphs.ToString

    intvbL = intParagraphs

    End Sub

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
    Me.Close()
    End Sub
    End Class

  4. #4
    Frenzied Member MrGTI's Avatar
    Join Date
    Oct 2000
    Location
    Ontario, Canada
    Posts
    1,277

    Thumbs up

    Hopefully this will help:
    VB Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         ''''''''''''''''''''''''''''''''''''''''''''
    3.         'Variable declaration section
    4.         ''''''''''''''''''''''''''''''''''''''''''''
    5.         Dim intWordCount As Integer = 0
    6.         Dim intSentences As Integer = 0
    7.         Dim intParagraphs As Integer = 0
    8.  
    9.         Dim WordsTotal As Integer
    10.         Dim intvbL As Integer
    11.         Dim count As Integer
    12.  
    13.         ''''''''''''''''''''''''''''''''''''''''''''
    14.         'Total Characters in text box section
    15.         ''''''''''''''''''''''''''''''''''''''''''''
    16.         'If InStr(txtTextScan.Text, Microsoft.VisualBasic.ControlChars.Lf) > 0 Then MsgBox("Found LineFeed Character(s).")
    17.         Dim sInputText As String = txtTextScan.Text.Replace(Microsoft.VisualBasic.ControlChars.CrLf, "")
    18.         'Uncomment these lines if you find you need/want them
    19.         'sInputText = sInputText.Replace(Microsoft.VisualBasic.ControlChars.Lf, "")
    20.         'sInputText = sInputText.Replace(Microsoft.VisualBasic.ControlChars.Cr, "")
    21.         Dim iCharacterCount As Integer = 0
    22.         If sInputText = "" Then
    23.             WordsTotal = iCharacterCount - intvbL
    24.             lblCharactersTotals.Text = WordsTotal.ToString
    25.         Else
    26.             iCharacterCount = sInputText.Length
    27.             lblCharactersTotals.Text = iCharacterCount.ToString
    28.         End If
    29.  
    30.         ''''''''''''''''''''''''''''''''''''''''''''
    31.         'Total Words in text box section
    32.         ''''''''''''''''''''''''''''''''''''''''''''
    33.         Dim strWords() As String
    34.         strWords = Split(txtTextScan.Text, ".")
    35.  
    36.         For count = 0 To UBound(strWords)
    37.             If txtTextScan.Text = " " Then
    38.                 intWordCount = 0
    39.                 If Len(strWords(count)) > 0 Then
    40.                     intWordCount = intWordCount + 1
    41.                 End If
    42.             End If
    43.         Next
    44.         lblWordsTotals.Text = intWordCount.ToString
    45.  
    46.         ''''''''''''''''''''''''''''''''''''''''''''
    47.         'Total Sentences in text box
    48.         ''''''''''''''''''''''''''''''''''''''''''''
    49.         Dim strLines() As String
    50.         strLines = Split(txtTextScan.Text, ".")
    51.  
    52.         For count = 1 To UBound(strLines)
    53.             If txtTextScan.Text = " " Then
    54.                 intSentences = 0
    55.             Else
    56.                 If Len(strLines(count)) > 0 Then
    57.                     intSentences = intSentences + 1
    58.                 End If
    59.             End If
    60.         Next
    61.         lblSentencesTotals.Text = intSentences.ToString
    62.  
    63.         ''''''''''''''''''''''''''''''''''''''''''''
    64.         'Total Paragraphs in text box section
    65.         ''''''''''''''''''''''''''''''''''''''''''''
    66.         Dim strParagraphs() As String
    67.         strLines = Split(txtTextScan.Text, Microsoft.VisualBasic.ControlChars.Lf)
    68.  
    69.         For count = 0 To UBound(strLines)
    70.             If Len(strLines(count)) > 0 Then
    71.                 intParagraphs = intParagraphs + 1
    72.             End If
    73.         Next
    74.         lblParagraphsTotals.Text = intParagraphs.ToString
    75.         intvbL = intParagraphs
    76.  
    77.     End Sub
    ~Peter


  5. #5

    Thread Starter
    Junior Member
    Join Date
    Feb 2003
    Posts
    27
    Thank you that did it

    I'm going to have to examine that code more closely. By the way is Len still considered to be a Vb.NET function? I used it all the time in 6 but just recently discovered VB.Net supports it. Makes alot of counting situations a lot easier.

  6. #6
    Frenzied Member MrGTI's Avatar
    Join Date
    Oct 2000
    Location
    Ontario, Canada
    Posts
    1,277

    Talking

    You're welcome. I only changed a few minor things. It's simple enough that i'm sure you'll see the differances quite readily.

    The Len function still exists in VB.NET, and it's fine to use it. You have already noticed that you can get the length of a string by using the built in Length function inherant to the .NET string class.
    ~Peter


  7. #7

    Thread Starter
    Junior Member
    Join Date
    Feb 2003
    Posts
    27
    Coluld you recommend another way or idea to count the number of characters or such if the Len function didn't exsist?

  8. #8
    Frenzied Member MrGTI's Avatar
    Join Date
    Oct 2000
    Location
    Ontario, Canada
    Posts
    1,277

    Wink

    Yes, the built in Length function that is a part of the String class:
    VB Code:
    1. Dim iLength As Integer = sText.Length
    (obviously sText is a string variable)
    ~Peter


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width