Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is best way to sort lines in RichTextBox

I'm looking for the best way to sort lines of RichTextBox, I'm using this at moment:

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. Is there an best way to do this?

  2. When I call it, the cursor is placed into first RichTextBox line, how do I to put it where it was before? I tried to set pStart/pEnd and CaretPositiom, but the properties are read only.

I hope this is clear. Thanks in advance.

like image 405
Jack Avatar asked Nov 14 '22 11:11

Jack


1 Answers

An inelegant but practical solution; back and forth richtextbox to ListBox : In the properties on your listBox you click 'sorted'> 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
like image 149
Eric Paroissien Avatar answered Nov 16 '22 03:11

Eric Paroissien