RichTextBox中对行进行排序的最佳方法是什么?

5

我正在寻找最佳的方法来对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和CaretPosition,但这些属性是只读的。

希望我的意思清楚了。非常感谢您提前的帮助。


3
你是按什么排序的(首字母、字母数量或其他)?你想让光标停留在相对位置不变吗(例如,如果有行A、B、C,光标位于B,然后你进行排序,新顺序为C、A、B,则光标应保持在B处还是移到A处)? - Jake Berger
4个回答

1
一个不太优雅但实用的解决方案; 使用来回操作richtextbox和listBox: 在listBox的属性中,单击“sorted”> true
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

0
感谢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);
}

0

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

0

就排序而言,这个解决方案与您建议的解决方案并没有太大的区别,但我发现它更加优雅,而且它处理了光标位置和选择:

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
@servy 这只是为了允许灵活更改排序标准。 - Avi Turner

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接