Solved

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

Posted on 2004-04-03
3
653 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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Hey Audio! 9 44
Please explain the parts of these 2 LINQ expressions 3 54
insert value of checklistbox checked 4 32
Calculate number of nights between two dates 5 54
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

828 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