Solved

Neat way to limit Textbox contents in C#. Maximum contents vs performance ?

Posted on 2004-04-03
3
648 Views
Last Modified: 2008-02-01
Hi

Is there a neat way in C# to limit the amount of data in a text box control. I use a textbox as a terminal window showing date time stamped information. Two questions...

1) What is the maximum chars I can have in a text box and where will I lose performance if it gets enormous
2) Does C# have a neater way of 'nibbling' off the earliest entries when a preset number of lines has been reached. No VS.Net on hand so pigeon code for current method is:

TextBox.Append myString;
if(TextBox.Length>10000){
  TextBox.Text=TextBox.SubString(1000);
}
0
Comment
Question by:nvms
3 Comments
 
LVL 15

Expert Comment

by:Timbo87
ID: 10750473
Set the MaxLength property of the textbox to whatever the maximum amount you want is. It's 32,767 by default, but you can even set it higher. The maximum number of letters is probably the maximum size of a string (VERY large, but I don't recall the exact amount).
0
 
LVL 4

Accepted Solution

by:
AndreSteffens earned 50 total points
ID: 10753309
The textbox has a Lines property which is an array of strings (provided the textbox is multiline of course)
what you could do is remove a line of text when the number of lines excees a certain number:
            private void textBox1_TextChanged(object sender, System.EventArgs e)
            {
                  if(textBox1.Lines.Length>10)
                  {
                        string[] newlines=new string[textBox1.Lines.Length-1];
                        int counter=0;
                        foreach(string line in textBox1.Lines)
                        {
                              if(counter>0)
                              {
                                    newlines[counter-1]=line;
                              }
                              counter++;
                        }
                        textBox1.Lines=newlines;
                        textBox1.SelectionStart=textBox1.Text.Length;//put the cursor at the end of the textbox
                  }
            }
When you set the AcceptsReturn property of the textbox to true you can easily test this, as you can use the enter key to create new lines in the textbox
0
 

Author Comment

by:nvms
ID: 10762628
Thanks Andre, that function works well, tested to 1000 lines and very little impact on performance. The earlier comment simply limits entry size where as Andre appreciated the requirement for a 'rolling buffer' of information.

I thought perhaps .Net had an intrinsic extension to the text handling code with this may not be the case

Cheers : )

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now