什么是在RichTextBox中排序行的最佳方法

时间:2012-01-25 19:56:16

标签: c# .net wpf richtextbox

我正在寻找排序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,但属性是只读的。

  3. 我希望这很清楚。提前谢谢。

4 个答案:

答案 0 :(得分:1)

一种不雅但实用的解决方案;来回富文本框到ListBox: 在listBox的属性中,单击“已排序”>真

并[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

答案 1 :(得分:0)

到目前为止,对于排序这个解决方案并没有与你建议的解决方案不同,但我发现它更优雅+它处理光标位置&amp;选择:

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;
        }
}

答案 2 :(得分:0)

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

Sub CreateList()
Dim i As Integer
Dim wsList As Worksheet
Dim ws As Worksheet
Set wsList = ActiveWorkbook.Sheets.Add

wsList.Name = "Compiled LOPA Data"
i = 1

For Each ws In ActiveWorkbook.Worksheets

If ws.Name <> "Compiled LOPA Data" Then
wsList.Cells(i, 1).Value = _
ws.Range("D3").Value


If ws.Name <> "Compiled LOPA Data" Then
wsList.Cells(i, 3).Value = _
ws.Range("B3").Value


If ws.Name <> "Compiled LOPA Data" Then
wsList.Cells(i, 4).Value = _
ws.Range("B6").Value


If ws.Name <> "Compiled LOPA Data" Then
wsList.Cells(i, 5).Value = _
ws.Range("A8").Value


If ws.Name <> "Compiled LOPA Data" Then
wsList.Cells(i, 6).Value = _
ws.Range("E7").Value

If ws.Name <> "Compiled LOPA Data" Then
wsList.Cells(i, 7).Value = _
ws.Range("E32").Value



If ws.Name <> "Compiled LOPA Data" Then
wsList.Cells(i, 8).Value = _
ws.Range("E6").Value

If ws.Name <> "Compiled LOPA Data" Then
wsList.Cells(i, 12).Value = _
ws.Range("A37").Value

If ws.Name <> "Compiled LOPA Data" Then
wsList.Cells(i, 13).Value = _
ws.Range("E37").Value

If ws.Name <> "Compiled LOPA Data" Then
wsList.Cells(i, 14).Value = _
ws.Range("A38").Value

If ws.Name <> "Compiled LOPA Data" Then
wsList.Cells(i, 15).Value = _
ws.Range("E38").Value


i = i + 1


End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If



Next ws

End Sub

答案 3 :(得分: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