Richtextbox-TextChanged事件和检查当前行中的空格

时间:2010-12-02 23:37:43

标签: c# events richtextbox textchanged

可以检查否。 richtextbox当前行中的空格及其文本更改事件... 例如

lin1: word1 word2 word3
lin2: word1 word2 word3 word4

when i type space after word3 it shows me message that you cannot enter word4

1 个答案:

答案 0 :(得分:1)

首先,使用此TextBox扩展并将其应用于RichTextBox(请参阅下面的代码,完整版本可查找here

public class TextBoxExtension : TextBox
{
 public TextBoxExtension (){ }

 public int CurrentLineIndex
 {
    get { return this.GetLineFromCharIndex(this.SelectionStart) + 1; }
 }
}

然后执行以下操作:

int maxWordsAllowed = 4;

private void textChangedEventHandler(object sender, TextChangedEventArgs args)
{    
    //This get the space character count in your current line 
    if (myRichTextBox.Lines[myRichTextBox.CurrentLineIndex].Split(' ').Length - 1) > maxWordsAllowed )
      MessageBox.Show("Reached Maximum Words for that line");    
}