Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2004-04-03
3
Medium Priority
?
658 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 200 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

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…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

636 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