在 C# 中从多行文本框的开头删除行

发布于 2024-08-17 04:05:24 字数 172 浏览 6 评论 0原文

C# 中是否有一种优雅的方法可以从多行文本框的开头删除多行文本?我正在使用 Microsoft Visual C# 2008 Express Edition。

编辑 - 其他详细信息 我的应用程序中的多行文本框被禁用(即它只能由应用程序本身编辑),并且每行都以“\r\n”结尾。

Is there a graceful way in C# to delete multiple lines of text from the beginning of a multiline textbox? I am using Microsoft Visual C# 2008 Express Edition.

EDIT - Additional Details
The multiline textbox in my application is disabled (i.e. it is only editable by the application itself), and every line is terminated with a "\r\n".

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

靑春怀旧 2024-08-24 04:05:24

这是一个不完整的问题。因此,假设您使用的是 TextBox 或 RichTextBox,您可以使用 在 TextBoxBase 中找到 Lines 属性

//get all the lines out as an arry
string[] lines = this.textBox.Lines;

然后您可以使用该数组并将其设置回来。

  this.textBox.Lines= newLinesArray;

这可能不是最优雅的方式,但它会删除第一行。
编辑:你不需要选择,只需使用skip就可以了

    //number of lines to remove from the beginning
    int numOfLines = 30; 
    var lines = this.textBox1.Lines;
    var newLines = lines.Skip(numOfLines);

    this.textBox1.Lines = newLines.ToArray();

This is an incomplete question. So assuming you are using either TextBox or RichTextBox you can use the Lines property found inTextBoxBase.

//get all the lines out as an arry
string[] lines = this.textBox.Lines;

You can then work with this array and set it back.

  this.textBox.Lines= newLinesArray;

This might not be the most elegant way, but it will remove the first line.
EDIT: you don't need select, just using skip will be fine

    //number of lines to remove from the beginning
    int numOfLines = 30; 
    var lines = this.textBox1.Lines;
    var newLines = lines.Skip(numOfLines);

    this.textBox1.Lines = newLines.ToArray();
小梨窩很甜 2024-08-24 04:05:24

此解决方案在 WPF 中适用于我:

while (LogTextBox.LineCount > Constants.LogMaximumLines)
{
    LogTextBox.Text = LogTextBox.Text.Remove(0, LogTextBox.GetLineLength(0));
}

您可以将 LogTextBox 替换为文本框的名称,将 Constants.LogMaximumLines 替换为您希望文本框具有的最大行数。

This solution works for me in WPF:

while (LogTextBox.LineCount > Constants.LogMaximumLines)
{
    LogTextBox.Text = LogTextBox.Text.Remove(0, LogTextBox.GetLineLength(0));
}

You can replace LogTextBox with the name of your text box, and Constants.LogMaximumLines with the maximum number of lines you would like your text box to have.

与他有关 2024-08-24 04:05:24

不幸的是,没有,无论您使用的是 ASP.NET、WinForms 还是 WPF/Silverlight,都没有“优雅”的方法从多行 TextBox 的文本中删除行。在每种情况下,您都会构建一个不包含不需要的行的字符串,并设置 Text 属性。

WinForms 将使用 Lines 属性将 Text 值预先分割成行,从而为您提供一些帮助,但这并不是很有帮助,因为它是一个字符串数组,并且删除数组的元素并不容易。

一般来说,该算法适用于 TextBox 类的所有可能版本:

var lines = (from item in myTextBox.Text.Split('\n') select item.Trim());
lines = lines.Skip(numLinesToSkip);
myTextBox.Text = string.Join(Environment.Newline, lines.ToArray());

注意:我专门针对 Unix 平台上的 Silverlight 情况使用了 Environment.Newline。对于所有其他情况,您可以在 string.Join 调用中使用“\r\n”。

另外,我不认为这是一个优雅的解决方案,尽管它只有 3 行。它的作用如下:

  • 将单个字符串拆分为字符串数组
  • ,迭代该数组并构建不包含跳过的行的第二个数组,
  • 将数组重新连接为单个字符串。

我不认为它很优雅,因为它本质上构建了两个单独的数组,然后从第二个数组构建一个字符串。更优雅的解决方案不会这样做。

Unfortunately, no, there is no "elegant" way to delete lines from the text of a multiline TextBox, regardless of whether you are using ASP.NET, WinForms, or WPF/Silverlight. In every case, you build a string that does not contain the lines you don't want and set the Text property.

WinForms will help you a little bit by pre-splitting the Text value into lines, using the Lines property, but it's not very helpful because it's a string array, and it's not exactly easy to delete an element of an array.

Generally, this algorithm will work for all possible versions of the TextBox class:

var lines = (from item in myTextBox.Text.Split('\n') select item.Trim());
lines = lines.Skip(numLinesToSkip);
myTextBox.Text = string.Join(Environment.Newline, lines.ToArray());

Note: I'm using Environment.Newline specifically for the case of Silverlight on a Unix platform. For all other cases, you're perfectly fine using "\r\n" in the string.Join call.

Also, I do not consider this an elegant solution, even though it's only 3 lines. What it does is the following:

  • splits the single string into an array of strings
  • iterates over that array and builds a second array that does not include the lines skipped
  • joins the array back into a single string.

I do not consider it elegant because it essentially builds two separate arrays, then builds a string from the second array. A more elegant solution would not do this.

难忘№最初的完美 2024-08-24 04:05:24

需要记住的一件事是,TextBox 的 Lines 集合不能准确反映用户所看到的线条。 Lines 集合基本上是靠回车来工作的,而用户可以看到没有回车的行从一行换行到下一行。这可能是也可能不是您想要的行为。

例如,用户将看到下面的三行,但 Lines 集合将显示 2(因为只有 2 个回车符):

This is line number 
one. 
This is line 2.

此外,如果表单和文本控件可调整大小,则文本中的可见行将发生变化随着控件的增大或缩小。

几年前,我写了一篇博客文章,介绍如何确定用户看到文本框中的行数并获取给定行的索引(例如获取索引处的行: http://ryanfarley.com/blog/archive/2004/04/07/511.aspx ,也许这篇文章会有所帮助。

One thing to keep in mind is that the Lines collection of the TextBox does not accurately reflect what the user sees as lines. The Lines collection basically works off of carriage returns, whereas the user could see lines wrapping from one line to the next without a carriage return. This may or may not be the behavior you want.

For example, the user would see the below as three lines, but the Lines collection will show 2 (since there are only 2 carriage returns):

This is line number 
one. 
This is line 2.

Also, if the form, and the text control are resizable the visible lines in the text will change as the control grows or shrinks.

I wrote a blog post several years ago on how to determine the number of lines in the textbox as the user sees them and get the index of a given line (like to get the line at index: http://ryanfarley.com/blog/archive/2004/04/07/511.aspx, perhaps this post will help.

嘿哥们儿 2024-08-24 04:05:24
if (txtLog.Lines.Length > maxNumberLines)
{
    txtLog.Lines = txtLog.Lines.Skip(txtLog.Lines.Length - maxNumberLines).ToArray();
}
if (txtLog.Lines.Length > maxNumberLines)
{
    txtLog.Lines = txtLog.Lines.Skip(txtLog.Lines.Length - maxNumberLines).ToArray();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文