Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在RichTextBox中排序行的最佳方法是什么_C#_.net_Wpf_Richtextbox - Fatal编程技术网

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

C# 在RichTextBox中排序行的最佳方法是什么,c#,.net,wpf,richtextbox,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, pE

我正在寻找对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);
}
  • 有没有最好的办法

  • 当我调用它时,光标被放在RichTextBox的第一行,我该如何将它放在之前的位置? 我试图设置pStart/pEnd和CaretPositiom,但属性是只读的


  • 我希望这是清楚的。提前感谢。

    就排序而言,此解决方案与您建议的解决方案没有什么不同,但我发现它更优雅+它可以处理光标位置和选择:

    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到ListBox: 在列表框的属性中,单击“已排序”>true

    [c#]


    感谢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);
    }
    
    ListBox1.Items.Clear();
    ListBox1.Items.AddRange(RichTextBox1.Lines);
    RichTextBox1.Clear();
    
    对于(int x=0;(xRichTextBox的工作方式类似于数组,我们可以使用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
    

    您排序的依据是什么(第一个字母、字母数或其他)?您是否希望光标放置在相同的相对位置(即,如果您有行A、B、C,光标位于B,则您进行排序,新的顺序为C、A、B,那么光标应保持在B还是保持在A)?为什么要将比较器传递到
    排序
    ?没有比较器就可以了?还有,这如何保持光标位置?@servy这只是为了允许灵活地更改排序标准。
    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);
    }
    
        Dim MyArray() As String
        MyArray = RichTextBox1.Lines
        Array.Sort(MyArray)
        RichTextBox1.Clear()
        For Each item In MyArray
            RichTextBox1.Text &= item & Environment.NewLine
        Next