0

我正在编写新颖的管理软件,我需要能够准确地跟踪richtextbox中的单词量。这是我到目前为止所拥有的

Dim Change As Integer
Dim Count As Integer

Private Sub RichTextBox1_TextChanged(sender As Object, e As EventArgs) Handles RichTextBox1.TextChanged
    Change = RichTextBox1.Text.Split(" ").ToLookup(Function(x) x).Count()
    'Add 1 to the variable Change every time the space bar is pressed

    Count = Change

    If RichTextBox1.Text = "" Then
        Count = 0
        'If the textbox is empty, the word count is 0
    ElseIf RichTextBox1.Text.EndsWith(" ") = True Then
        Count = Change - 1
        'take one away from the wordcount variable when the last character is a space
    End If

    ToolStripStatusLabel2.Text = Count
    'Display the wordcount
End Sub

如何让代码继续在多行上运行?到目前为止,代码只在第一行的文本上运行。如果用户按回车键然后继续输入,则字数不计算后续每行的第一个字

4

1 回答 1

0

您可以在 keydown 处理程序中使用类似的东西。前导和尾随空格可能有一些特殊情况,除非它可以关闭 1,并且应该处理退格。

If e.KeyValue = Keys.Space Or e.KeyValue = Keys.Back Then
  ss = rText1.Text.Split
  txCount.Text = UBound(ss) + 1
  End If
于 2014-12-11T19:37:35.357 回答