CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Apr 2012
    Posts
    3

    help word count using array in visual basic 6

    need help to find code to count words inside a textbox using visual basic 6
    i tried this code but there was an error
    Private Sub Button1_Click(ByVal sender As System.Object,

    ByVal e As System.EventArgs) Handles Button1.Click
    Dim str As String
    Dim i, l, words As Integer
    str = TextBox1.Text
    str = LTrim(str) 'removes leading blank spaces
    str = RTrim(str) ' removes trailing blank spaces
    l = str.Length
    i = 0
    words = 0
    While (i < l)
    If str(i) = " " Then
    words = words + 1
    i = i + 1
    While str(i) = " " ' removes continuous blank spaces
    i = i + 1
    End While
    Else
    i = i + 1
    End If

    End While
    words = words + 1 ' adds the last word
    MessageBox.Show("WORDS =" & words)
    End Sub

    please help

  2. #2
    Join Date
    Sep 2006
    Posts
    635

    Re: help word count using array in visual basic 6

    Hi
    Here's VB 6. forum.

    I see your source code is vb .Net

  3. #3
    Join Date
    Apr 2012
    Posts
    3

    Re: help word count using array in visual basic 6

    is it possible if you could change it to vb6 code?

  4. #4
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: help word count using array in visual basic 6

    Well in Vb6 you might go this way:
    Code:
      dim st$
      st=Trim$(TextBox.Text) 'remove leading and trailing blaks in one go
      Dim a$()
      a = Split(st, " ") 'split the text at every blank
      dim words&#37;
      words = UBound(a)+1 'number of words is the number of elements resulting from the split

  5. #5
    Join Date
    Apr 2012
    Posts
    3

    Re: help word count using array in visual basic 6

    this is using array right? can it be done with array and for loop / if else?


    hello voon
    Last edited by chickenguru; April 6th, 2012 at 10:25 PM.

  6. #6
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: help word count using array in visual basic 6

    Yes. A() is the array. You'd loop AFTER splitting into the array. Give it a try.

    Words is the UPPER BOUND of the array, so you'd loop from 0 to Max-1
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  7. #7
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: help word count using array in visual basic 6

    Why would you use a for loop and if/then/else constructs, when the Split() functions delivers a nice array with all the single words of a text?
    You can afterwards, as David explained, use any kind of loop to walk through the elements of the array.

Tags for this Thread

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

Featured