【问题标题】:Richtext Box-TextChanged Even and checking spaces in current lineRichtextbox-TextChanged 事件和检查当前行中的空格
【发布时间】:2010-12-02 23:37:42
【问题描述】:

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

lin1: word1 word2 word3
lin2: word1 word2 word3 word4

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

【问题讨论】:

    标签: c# events richtextbox textchanged


    【解决方案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");    
    }
    

    【讨论】:

      猜你喜欢
      • 2014-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多