是否可以检查没有。富文本框当前行中的空格及其文本更改事件...例如
lin1: word1 word2 word3
lin2: word1 word2 word3 word4
when i type space after word3 it shows me message that you cannot enter word4
是否可以检查没有。富文本框当前行中的空格及其文本更改事件...例如
lin1: word1 word2 word3
lin2: word1 word2 word3 word4
when i type space after word3 it shows me message that you cannot enter word4
首先,使用此 TextBox 扩展并将其应用到您的 RichTextBox(请参见下面的代码,完整版本可在此处找到)
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");
}