gpt4 book ai didi

c# - 在 RichTextBox 中对行进行排序的最佳方法是什么

转载 作者:太空狗 更新时间:2023-10-29 23:47:37 24 4
gpt4 key购买 nike

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

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

最佳答案

一个不优雅但实用的解决方案;来回 richtextbox 到 ListBox :在列表框的属性中,单击“已排序”> 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

关于c# - 在 RichTextBox 中对行进行排序的最佳方法是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9009353/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com