Solved

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

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

816 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

11 Experts available now in Live!

Get 1:1 Help Now