home *** CD-ROM | disk | FTP | other *** search
/ Mastering Visual Basic 6 / mastvb6.iso / ch_code / ch03 / wcount / wcount.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1998-06-28  |  2.7 KB  |  85 lines

  1. VERSION 5.00
  2. Begin VB.Form WordCountForm 
  3.    Caption         =   "Counting Words"
  4.    ClientHeight    =   4485
  5.    ClientLeft      =   60
  6.    ClientTop       =   345
  7.    ClientWidth     =   6975
  8.    LinkTopic       =   "Form1"
  9.    ScaleHeight     =   4485
  10.    ScaleWidth      =   6975
  11.    StartUpPosition =   3  'Windows Default
  12.    Begin VB.CommandButton Command1 
  13.       Caption         =   "Count Words"
  14.       BeginProperty Font 
  15.          Name            =   "Verdana"
  16.          Size            =   9
  17.          Charset         =   0
  18.          Weight          =   400
  19.          Underline       =   0   'False
  20.          Italic          =   0   'False
  21.          Strikethrough   =   0   'False
  22.       EndProperty
  23.       Height          =   495
  24.       Left            =   4800
  25.       TabIndex        =   1
  26.       Top             =   3840
  27.       Width           =   2055
  28.    End
  29.    Begin VB.TextBox Text1 
  30.       BeginProperty Font 
  31.          Name            =   "Verdana"
  32.          Size            =   9
  33.          Charset         =   0
  34.          Weight          =   400
  35.          Underline       =   0   'False
  36.          Italic          =   0   'False
  37.          Strikethrough   =   0   'False
  38.       EndProperty
  39.       Height          =   3495
  40.       Left            =   120
  41.       MultiLine       =   -1  'True
  42.       TabIndex        =   0
  43.       Text            =   "WCount.frx":0000
  44.       Top             =   120
  45.       Width           =   6735
  46.    End
  47. Attribute VB_Name = "WordCountForm"
  48. Attribute VB_GlobalNameSpace = False
  49. Attribute VB_Creatable = False
  50. Attribute VB_PredeclaredId = True
  51. Attribute VB_Exposed = False
  52. Private Sub Command1_Click()
  53. Dim position As Long
  54. Dim words As Long
  55. Dim myText As String
  56.     position = 1
  57.     myText = Text1.Text
  58.     ' massage string:
  59.     ' replace line feeds with spaces
  60.     myText = Replace(myText, Chr(13) & Chr(10), " ")
  61.     ' replace tabs with single spaces
  62.     myText = Replace(myText, Chr(9), " ")
  63.     myText = Trim(myText)
  64.     ' Count the first word
  65.     ' Because the last word isn't delimited by
  66.     ' a space, if the string isn't blank, then it
  67.     ' contains at least one word.
  68.     ' By setting words=1, we won't have to increase the
  69.     ' number of words by 1 when we are done counting.
  70.     If Len(myText) > 0 Then words = 1
  71.     ' while the string contains spaces...
  72.     Do While position > 0
  73.         position = InStr(position, myText, " ")
  74.         ' ... increase word count
  75.         If position > 0 Then
  76.             words = words + 1
  77.             ' and skip additional spaces
  78.             While Mid(myText, position, 1) = " "
  79.                 position = position + 1
  80.             Wend
  81.         End If
  82.     Loop
  83.     MsgBox "The TextBox contains " & words & " words"
  84. End Sub
  85.