问题描述
我在寻找排序的RichTextBox的线最好的方法,我用这个的时刻:
公共无效SortLines(对象发件人,EventArgs的)
{
TextPointer PSTART = TextInput.Document.ContentStart;
TextPointer小彭= TextInput.Document.ContentEnd;
TextRange的文本=新的TextRange(PSTART,小彭);
字符串[]行= text.Text.Split('\ N');
的Array.Sort(线);
text.Text =的string.join(的String.Empty,行);
}
-
有没有做到这一点的最好方法是什么?
-
当我把它称为,将光标置于第一RichTextBox的线,我怎么把它放在它面前?我试图设置PSTART /小彭和CaretPositiom,但属性为只读。
我希望这是显而易见的。先谢谢了。
据是涉及到分拣这个解决方案是不是与你不同建议,但我觉得它更优雅+它处理光标位置和放大器;选择:
公共无效SortLines(对象发件人,EventArgs的)
{
rtb.HideSelection = FALSE; //用于显示选择
/ *保存当前的选择* /
字符串selectedText = rtb.SelectedText;
/ *保存CURR行* /
INT firstCharInLineIndex = rtb.GetFirstCharIndexOfCurrentLine();
INT currLineIndex = rtb.Text.Substring(0,firstCharInLineIndex).Count之间(C =以及c =='\ N');
字符串currLine = rtb.Lines [currLineIndex]
INT偏移= rtb.SelectionStart -firstCharInLineIndex;
/ *排序* /
字符串[]行= rtb.Lines;
的Array.Sort(系,委托(串STR1,字符串STR2){返回str1.CompareTo(STR2);});
rtb.Lines =行;
如果(!String.IsNullOrEmpty((selectedText)))
{
/ *恢复选择* /
INT newIndex = rtb.Text.IndexOf(selectedText);
rtb.Select(newIndex,selectedText.Length);
}
其他
{/ *恢复光标* /
//当前行的位置
INT lineIdx = Array.IndexOf(rtb.Lines,currLine);
INT textIndex = rtb.Text.IndexOf(currLine);
INT fullIndex = textIndex +偏移;
rtb.SelectionStart = fullIndex;
rtb.SelectionLength = 0;
}
}
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);
}
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?I tried to set pStart/pEnd and CaretPositiom, but the properties are read only.
I hope this is clear. Thanks in advance.
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;
}
}
这篇关于什么是排序RichTextBox的线最好的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!