vb字计数器richtextbox覆盖多行

时间:2014-12-11 12:51:04

标签: vb.net count ms-word richtextbox writing

我正在编写新颖的管理软件,我需要能够准确地跟踪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

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

1 个答案:

答案 0 :(得分: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