简体   繁体   English

添加新文本框时删除文本框的第一行

[英]Removing the first line of a textbox when a new one gets added

I am using the following command to append text to a multiline textbox that I am using: 我正在使用以下命令将文本追加到正在使用的多行文本框中:

TextBox1.AppendText(Environment.NewLine & "Current status : " & currentStatus)

However, if there are (for example) 49 lines, I want to delete the first line when line 50 gets added. 但是,如果有(例如)49行,我想在添加第50行时删除第一行。

How can I do this ? 我怎样才能做到这一点 ?

You can also use LINQ to solve this problem. 您也可以使用LINQ解决此问题。 Here i have used SKIP and TAKE functions. 这里我用SKIPTAKE功能。

If textBox1.Lines.Count = 50 Then
    textBox1.Lines= textBox1.Lines.Skip(Of String)(1).Take(textBox1.Lines.Length - 1).ToArray()
End If

NOTE: It will only work if you have framework 3.5 or above. 注意:仅当您具有框架3.5或更高版本时,它才有效。

Try this 尝试这个

dim sNewLine as String '-------> inserted text 
dim s as string = textbox1.text

s=mid(s,instr(s,vbCRLF)+2)

To append new line 追加新行

textbox1.text = s & vbCRLF & sNewLine

The text box has a TextChanged event, which is fired every time the text changes. 文本框具有TextChanged事件,每次文本更改时都会触发该事件。

You can use this to count the number of lines in the text box, which is the same as the number of newline characters. 您可以使用它来计算文本框中的行数,该行数与换行符的数目相同。 If this amount exceeds your limit, you can remove the first line by locating the first newline character and remove it and everything in front of it. 如果该数量超出限制,则可以通过找到第一个换行符来删除第一行,并将其及其前面的所有内容都删除。

I don't speak VB.Net, so I hope you can work with this and write the code. 我不会说VB.Net,所以希望您可以使用它并编写代码。

I made an attempt any way: 我以任何方式尝试:

Private Sub textBox1_TextChanged(sender As Object, e As EventArgs)
    Dim lineCount As Integer = textBox1.Lines.Count()

    If lineCount > 49 Then
        textBox1.Text = textBox1.Text.Substring(textBox1.Text.IndexOf(Environment.NewLine))
    End If
End Sub

You could try something like this: 您可以尝试这样的事情:

If TextBox1.Lines.Count = 50 Then
    Dim temp As String(50)
    Array.Copy(TextBox1.Lines,1,temp,0,49)
    TextBox1.Lines = temp
End If

TextBox1.AppendText(Environment.NewLine & "Current status : " & currentStatus)

I'm running on brain compiler and an iPhone at the moment, but I think that should work. 目前,我正在运行大脑编译器和iPhone,但我认为这应该可行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM