【问题标题】:Delete the last line of rich Text Box?删除富文本框的最后一行?
【发布时间】:2014-03-23 05:36:33
【问题描述】:

我喜欢删除以 ; 结尾的富文本框的最后一行半栏。我喜欢删除这一行,直到;最后一个半列之前的半列。

示例:

hello do not delete this line;
hello this sentence will continue...
untill here;

结果应该是:

hello do not delete this line;

我的代码:

private void button1_Click_1(object sender, EventArgs e) {
        List<string> myList = richTextBox1.Lines.ToList();
        if (myList.Count > 0) {
            myList.RemoveAt(myList.Count - 1);
            richTextBox1.Lines = myList.ToArray();
            richTextBox1.Refresh();
        }
    }

【问题讨论】:

    标签: c# regex substring stringbuilder


    【解决方案1】:

    找到解决方案here:

    RichTextBox1.Lines = RichTextBox1.Lines.Take(RichTextBox1.Lines.Length - 3).ToArray();
    

    【讨论】:

      【解决方案2】:

      使用这个:

      var last = richTextBox1.Text.LastIndexOf(";");
      if (last > 0)
      {
         richTextBox1.Text = richTextBox1.Text.Substring(0, last - 1);
         var beforelast = richTextBox1.Text.LastIndexOf(";");
         richTextBox1.Text = richTextBox1.Text.Substring(0, beforelast + 1);
      }
      else
      {
         richTextBox1.Text = "";
      }
      

      您没有指定其他场景(即,当字符串不包含“;”时) 此代码删除以“;”开头的字符串就在最后一个“;”之前到最后一个“;”。 它删除最后一个分号和之后的文本,然后找到新的最后一个“;”。最后删除此“;”之后的文本

      【讨论】:

      • 这会从现有文本中删除格式。而是使用 SelectedText 方法。
      【解决方案3】:

      对于那些多年后发现这个问题的人......

      使用 .Text 属性或 .Lines 属性的解决方案最终会从现有文本中删除格式。而是使用这样的东西来保留格式:

      var i = textBox.Text.LastIndexOf("\n");
      textBox.SelectionStart = i;
      textBox.SelectionLength = o.TextLength - i + 1;
      textBox.SelectedText = "";
      

      请注意,如果您的文本框处于只读模式,则无法修改 SelectedText。在这种情况下,您需要像这样设置和重置 ReadOnly:

      textBox.ReadOnly = false;
      textBox.SelectedText = "";
      textBox.ReadOnly = true;
      

      【讨论】:

        【解决方案4】:

        我不确定富文本框的工作原理,但类似于

        input = {rich text box text}
        int index = text.lastIndexOf(";");
        if (index > 0) 
        {
            input = input.Substring(0, index);
        }
        
        // put input back in text box
        

        【讨论】:

        【解决方案5】:

        怎么样?

        string input = "your complete string; Containing two sentences";
        
        List<string> sentences = s.Split(';').ToList();
        
        //Delete the last sentence
        sentences.Remove(sentences[sentences.Count - 1]);
        
        string result = string.Join(" ", sentences.ToArray());
        

        【讨论】:

          【解决方案6】:
          int totalcharacters = yourrtb.Text.Trim().Length;
          int totalLines = yourrtb.Lines.Length;
          string lastLine = yourrtb.Lines[totalLines - 1];
          int lastlinecharacters = lastLine.Trim().Length;
          yourrtb.Text = yourrtb.Text.Substring(0, totalcharacters - lastlinecharacters);
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多