Results 1 to 2 of 2

Thread: stop textbox to grow too big

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2010
    Posts
    69

    stop textbox to grow too big

    Hi,

    I've got a textbox that displays a string received via RS232.
    Every second there is a new string of data, that is added to the textbox.

    How do I keep the textbox to a limit, so it won't grow too big and eat memory up?
    But at the same time, I want to keep the latest strings visible in the window.

    I tried
    If txt_inc.TextLength > 512Then
    txt_inc.Text = ""
    End If
    but that it's clearing the textbox completley, as opposed to leave what you can see in the window untouched, and delete only what is been masked by the scrollbars.

    anybody?

    thanks.

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    104,969

    Re: stop textbox to grow too big

    You would start by setting the MaxLength property of the TextBox appropriately. Here's a VB translation (courtesy of Instant VB from Tangible Software Solutions) of a C# function I wrote for someone else on this forum just two days ago that will add a line to a TextBox and delete lines from the top as required to ensure that the TextLength never exceeds the MaxLength:
    Code:
    Private Sub AddNewLine(ByVal newLine As String)
    	If textBox1.TextLength = 0 Then
    		' This is the first line.
    		textBox1.Text = newLine
    	Else
    		' The number of lines to remove.
    		Dim skipCount = 0
    
    		' The length of the intended text including a line break.
    		Dim textLength = textBox1.TextLength + newLine.Length + 2
    
    		Dim maxTextLength = textBox1.MaxLength
    		Dim lines = textBox1.Lines
    
    		' Count the number of lines to be removed.
    		Do While textLength > maxTextLength
    			' The next line will be removed.
    			textLength -= lines(skipCount).Length + 2
    			skipCount += 1
    		Loop
    
    		' Add the new line and remove initial lines as necessary.
    		textBox1.Lines = textBox1.Lines.Skip(skipCount).Concat(New String()  { newLine }).ToArray()
    	End If
    End Sub
    Note that that requires .NET 3.5.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width