Solved

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

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

757 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

21 Experts available now in Live!

Get 1:1 Help Now