[英]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:我正在寻找对 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);
}
Is there an best way to do this?有没有最好的方法来做到这一点?
When I call it, the cursor is placed into first RichTextBox line, how do I to put it where it was before?当我调用它时,光标被放置在第一个 RichTextBox 行中,如何将它放在之前的位置? I tried to set pStart/pEnd and CaretPositiom, but the properties are read only.
我尝试设置 pStart/pEnd 和 CaretPositiom,但这些属性是只读的。
I hope this is clear.我希望这很清楚。 Thanks in advance.
提前致谢。
An inelegant but practical solution;一个不优雅但实用的解决方案; back and forth richtextbox to ListBox : In the properties on your listBox you click 'sorted'> true
来回富文本框到列表框:在列表框的属性中,您单击“已排序”> true
[c#] [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] [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
As far is it comes to sorting this solution is not to different from you suggested one, but I find it more elegant + it handles cursor location & selection:就排序而言,此解决方案与您建议的解决方案没有什么不同,但我发现它更优雅 + 它处理光标位置和选择:
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;
}
}
Thanks Eric Paroissien for your simple solution!感谢 Eric Paroissien 的简单解决方案! The C# code had a couple of issues - Here's his solution with the fix
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);
}
RichTextBox works like an array, we can use array.sort this way : 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.