【问题标题】:what is best way to sort lines in RichTextBox在 RichTextBox 中对行进行排序的最佳方法是什么
【发布时间】:2021-03-02 00:34:23
【问题描述】:

我正在寻找对 RichTextBox 的行进行排序的最佳方法,我现在正在使用这个:

public void SortLines(object sender, EventArgs e)
{
    TextPointer pStart = TextInput.Document.ContentStart;
    TextPointer pEnd = TextInput.Document.ContentEnd;
    TextRange text = new TextRange(pStart, pEnd);

    string[] lines = text.Text.Split('\n');
    Array.Sort(lines);
    text.Text = String.Join(String.Empty, lines);
}
  1. 有没有最好的方法来做到这一点?

  2. 当我调用它时,光标被放置在第一行 RichTextBox 中,我如何将它放在它之前的位置? 我尝试设置 pStart/pEnd 和 CaretPositiom,但属性是只读的。

我希望这很清楚。提前致谢。

【问题讨论】:

  • 您按什么排序(第一个字母、字母数或其他)?您是否希望将光标放置在相同的相对位置(即,如果您有 A、B、C 行并且光标位于 B,那么您排序并且新顺序是 C、A、B,那么光标是否应该留在 B还是留在A)?

标签: c# .net wpf richtextbox


【解决方案1】:

一个不优雅但实用的解决方案;来回 Richtextbox 到 ListBox : 在 listBox 的属性中,单击“排序”> true

[c#]

ListBox1.Items.AddRange(RichTextBox1.Lines);
for (int x = 0; (x 
            <= (ListBox1.Items.Count - 1)); x++) {
    RichTextBox1.AppendText((ListBox1.Items(x).ToString.Environment.NewLine));
}

[VB.NET]

ListBox1.Items.AddRange(RichTextBox1.Lines)
For x As Integer = 0 To ListBox1.Items.Count - 1
RichTextBox1.AppendText(ListBox1.Items(x).ToString & Environment.NewLine)
Next

【讨论】:

    【解决方案2】:

    就排序而言,这个解决方案与您建议的解决方案没有什么不同,但我发现它更优雅 + 它处理光标位置和选择:

    public void SortLines(object sender, EventArgs e)
    {
           rtb.HideSelection = false; //for showing selection
            /*Saving current selection*/
            string selectedText = rtb.SelectedText;
            /*Saving curr line*/
            int firstCharInLineIndex = rtb.GetFirstCharIndexOfCurrentLine();
            int currLineIndex = rtb.Text.Substring(0, firstCharInLineIndex).Count(c => c == '\n');
            string currLine = rtb.Lines[currLineIndex];
            int offset = rtb.SelectionStart -firstCharInLineIndex;
    
    
            /*Sorting*/
            string[] lines = rtb.Lines;
            Array.Sort(lines, delegate(string str1, string str2) { return str1.CompareTo(str2); });
            rtb.Lines = lines;
    
            if (!String.IsNullOrEmpty((selectedText)))
            {
                /*restoring selection*/
                int newIndex = rtb.Text.IndexOf(selectedText);
                rtb.Select(newIndex, selectedText.Length);
            }
            else
            {   /*Restoring the cursor*/
    
                //location of the current line
                int lineIdx = Array.IndexOf(rtb.Lines, currLine);
                int textIndex = rtb.Text.IndexOf(currLine);
                int fullIndex = textIndex + offset;
                rtb.SelectionStart =  fullIndex;
                rtb.SelectionLength = 0;
            }
    }
    

    【讨论】:

    • 为什么要将比较器传递给Sort?没有它会很好吗?另外,这如何保持光标位置?
    • @servy 这只是为了允许灵活地更改排序标准。
    【解决方案3】:

    感谢 Eric Paroissien 提供的简单解决方案! C# 代码有几个问题 - 这是他的修复解决方案

    ListBox1.Items.Clear();
    ListBox1.Items.AddRange(RichTextBox1.Lines);
    RichTextBox1.Clear();
    for (int x = 0; (x <= (ListBox1.Items.Count - 1)); x++)
    {
        RichTextBox1.AppendText(ListBox1.Items[x].ToString());
        RichTextBox1.AppendText(Environment.NewLine);
    }
    

    【讨论】:

      【解决方案4】:

      RichTextBox 就像一个数组,我们可以这样使用 array.sort :

          Dim MyArray() As String
          MyArray = RichTextBox1.Lines
          Array.Sort(MyArray)
          RichTextBox1.Clear()
          For Each item In MyArray
              RichTextBox1.Text &= item & Environment.NewLine
          Next
      

      【讨论】:

        猜你喜欢
        • 2013-07-07
        • 1970-01-01
        • 2010-09-18
        • 2010-12-16
        • 2021-06-09
        • 2010-09-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多