VB2010(15)_一个简单的统计字符或单词数的应用程序

VB2010(15)_一个简单的统计字符或单词数的应用程序

Public Class Form1
    '统计字符数的函数
    Private Function CountCharcters(ByVal text As String) As Integer
        Return text.Length
    End Function
    '统计单词数的函数
    Private Function CountWords(ByVal text As String) As Integer
        If text.Trim.Length = 0 Then Return 0
        Dim strWords() As String = text.Split(" "c)
        Return strWords.Length
    End Function
    '从文本框中获取文本并更新显示的子过程
    Private Sub UpdateDisplay()
        If radCountWords.Checked Then
            lblResults.Text = CountWords(txtWords.Text) & " words."
        Else
            lblResults.Text = CountCharcters(txtWords.Text) & " characters."
        End If
    End Sub
    '文本框事件
    Private Sub txtWords_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtWords.TextChanged
        UpdateDisplay()
    End Sub
    'radioButton事件
    Private Sub radCountWords_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles radCountWords.CheckedChanged
        UpdateDisplay()
    End Sub
    'RadioButton事件
    Private Sub radCountChars_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles radCountChars.CheckedChanged
        UpdateDisplay()
    End Sub

End Class