Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents txtWords As System.Windows.Forms.TextBox
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents radCountChars As System.Windows.Forms.RadioButton
    Friend WithEvents radCountWords As System.Windows.Forms.RadioButton
    Friend WithEvents Label2 As System.Windows.Forms.Label
    Friend WithEvents lblResults As System.Windows.Forms.Label
    Friend WithEvents btnShowMe As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.txtWords = New System.Windows.Forms.TextBox()
        Me.Label1 = New System.Windows.Forms.Label()
        Me.radCountChars = New System.Windows.Forms.RadioButton()
        Me.radCountWords = New System.Windows.Forms.RadioButton()
        Me.Label2 = New System.Windows.Forms.Label()
        Me.lblResults = New System.Windows.Forms.Label()
        Me.btnShowMe = New System.Windows.Forms.Button()
        Me.SuspendLayout()
        '
        'txtWords
        '
        Me.txtWords.Location = New System.Drawing.Point(8, 32)
        Me.txtWords.Multiline = True
        Me.txtWords.Name = "txtWords"
        Me.txtWords.ScrollBars = System.Windows.Forms.ScrollBars.Vertical
        Me.txtWords.Size = New System.Drawing.Size(352, 152)
        Me.txtWords.TabIndex = 0
        Me.txtWords.Text = ""
        '
        'Label1
        '
        Me.Label1.Location = New System.Drawing.Point(8, 8)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(160, 23)
        Me.Label1.TabIndex = 1
        Me.Label1.Text = "Enter some text into this box…"
        '
        'radCountChars
        '
        Me.radCountChars.Checked = True
        Me.radCountChars.Location = New System.Drawing.Point(8, 192)
        Me.radCountChars.Name = "radCountChars"
        Me.radCountChars.Size = New System.Drawing.Size(56, 32)
        Me.radCountChars.TabIndex = 2
        Me.radCountChars.TabStop = True
        Me.radCountChars.Text = "Chars"
        '
        'radCountWords
        '
        Me.radCountWords.Location = New System.Drawing.Point(64, 192)
        Me.radCountWords.Name = "radCountWords"
        Me.radCountWords.Size = New System.Drawing.Size(56, 32)
        Me.radCountWords.TabIndex = 3
        Me.radCountWords.Text = "Words"
        '
        'Label2
        '
        Me.Label2.Location = New System.Drawing.Point(128, 200)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(88, 16)
        Me.Label2.TabIndex = 4
        Me.Label2.Text = "The results are:"
        '
        'lblResults
        '
        Me.lblResults.Location = New System.Drawing.Point(208, 200)
        Me.lblResults.Name = "lblResults"
        Me.lblResults.Size = New System.Drawing.Size(80, 16)
        Me.lblResults.TabIndex = 5
        '
        'btnShowMe
        '
        Me.btnShowMe.Location = New System.Drawing.Point(296, 192)
        Me.btnShowMe.Name = "btnShowMe"
        Me.btnShowMe.Size = New System.Drawing.Size(64, 24)
        Me.btnShowMe.TabIndex = 6
        Me.btnShowMe.Text = "Show Me"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(368, 229)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.btnShowMe, Me.lblResults, Me.Label2, Me.radCountWords, Me.radCountChars, Me.Label1, Me.txtWords})
        Me.Name = "Form1"
        Me.Text = "Word Counter"
        Me.ResumeLayout(False)

    End Sub

#End Region


    ' CountCharacters - count the characters in a block of text...
    Public Function CountCharacters(ByVal text As String) As Integer
        ' return the number of characters...
        Return text.Length
    End Function


    ' CountWords - count the number of words in a block of text...
    Public Function CountWords(ByVal text As String) As Integer

        ' is the text box empty?
        If txtWords.Text = "" Then Return 0

        ' split...
        Dim words() As String = text.Split(" ".ToCharArray())
        Return words.Length

    End Function


    ' UpdateDisplay - update the display...
    Public Function UpdateDisplay() As String
        ' what text do we want to use?
        Dim countText As String = txtWords.Text
        Dim resultText As String

        ' do we want to count words?
        If radCountWords.Checked = True Then
            ' count the words...
            Dim numWords As Integer = CountWords(countText)

            ' return the text...
            resultText = numWords & " words"
        Else
            ' count the chars...
            Dim numChars As Integer = CountCharacters(countText)

            ' return the text...
            resultText = numChars & " characters"
        End If
        ' update the display...
        lblResults.Text = resultText
    End Function


    Private Sub txtWords_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtWords.TextChanged
        ' something's changed... update the display...
        UpdateDisplay()
    End Sub


    Private Sub radCountWords_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles radCountWords.CheckedChanged
        ' something's changed... update the display...
        UpdateDisplay()
    End Sub


    Private Sub radCountChars_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles radCountChars.CheckedChanged
        ' something's changed... update the display...
        UpdateDisplay()
    End Sub

    Private Sub btnShowMe_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnShowMe.Click
        ' display the text contained in the label...
        MessageBox.Show(lblResults.Text, "Word Counter")
    End Sub
End Class
