Solved

.NET WinForms TextBox appears empty when it reaches a certain length (of data)

Posted on 2012-03-29
9
462 Views
Last Modified: 2012-04-22
When a windows forms TextBox reaches a certain length of text, it will start to appear empty in your form.

This is very simple to reproduce, but the exact number of characters (length) required to make the text disappear changes system to system - probably something to do with memory.

To reproduce this issue:

Open VS2010
Create new Windows Forms application
Place 1 textbox, 1 label, 1 button on the form. Leave the default names.

Place the following code in the buttons click event:

for (int x = 0; x < 1000; x++)
                textBox1.Text = textBox1.Text + "b";
            
            label1.Text = textBox1.Text.Length.ToString();

Open in new window


Then run the app and hit the button until the text disappears. For me it was at 32000 characters.

Now... I need a way to stop that text from disappearing. We cannot set the textbox to multi-line for technical reasons - the textboxes in question are single line textboxes that sometimes exceed 10k characters, but only the first 50-100 characters are visible on the form... but we need those first few characters to be visible so the customer is not confused.

Any ideas?

I am attaching a sample project.. tho it may be faster to follow instructions above and create a new one lol.
0
Comment
  • 5
  • 2
  • 2
9 Comments
 

Author Comment

by:SimplePaymentSolutions
ID: 37783596
Oh, sorry - let me add. Tho it appears empty, you can still click on the textbox, hit CTRL-A + CTRL-C to copy then paste the data in a notepad or wherever...  and using the TextBox.Text property still works fine - nothing is lost... it just "looks" empty to the user.
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 37783882
Stupid question...why bother loading 10k characters into a single line TextBox?!

What usefulness does that give to the user?  Are they painfully scrolling thru the contents?

How is the data being used?...and how does the user interact with it?

Could you instead display a fixed number of characters and provide "paging" controls to change the contents?
0
 

Author Comment

by:SimplePaymentSolutions
ID: 37783952
Most clients only have a small number of characters in the effected fields...

as to its use... that varies. This is for a HUGE application, with literally hundreds upon hundreds of single line text boxes, the vast majority of which will never grow beyond whats visible.

We allow a user to use a kind of scripting language in some of these fields that allow them to add very complex sets of data to these textboxes... and while it is not necessary to display it all to the user, it IS necessary to display SOMETHING so that the user does not think its empty and "lost".

When the data is used it works properly...

As to displaying a fixed number of characters, sure... if you know of a way to do that that would not require me to change all my textboxes to some 3rd party control that allows paging - as far as I know that option is not available on the standard .NET Textbox.

If there is a way to extend the textbox to add that ability, then fine... that would work.. so long as the user retains the ability to CTRL-A to get everything and copy it to the clipboard for manipulation in other areas of the application when necessary.

All in all it would be much better to remove this limitation from the textbox I think...

Only a small number of our clients have this problem, so its not worth our time in moving to a very complex solution... we need something simple.

Any ideas?


Dave
0
 
LVL 23

Accepted Solution

by:
wdosanjos earned 500 total points
ID: 37784018
The only way to do this it to set textBox1.Multiline = true.  You don't need to change the way the textbox is displayed on the screen, so it can continue to be a single line.

Can you elaborate on the 'We cannot set the textbox to multi-line for technical reasons' limitation?

Thanks.
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Comment

by:SimplePaymentSolutions
ID: 37784033
Simple - having a linebreak in the value of .Text would crash several parts of our application... and I would prefer not having to validate the value of .Text for linebreaks in every place they are used... The whole idea here is to try to find a simple fix - one that will not take me 20+ hours to implement (as I said, there are a LOT of these textboxes... hundreds upon hundreds spread across hundreds of forms.)

Dave
0
 
LVL 23

Expert Comment

by:wdosanjos
ID: 37784247
OK. The following code should take care of both issues (display long strings and prevent linebreaks on TextBoxes).  The code relies on the OnLoad event of the form to look for all TextBoxes, set the Multiline property to true, and hook up the event handler to prevent linebreaks from being accepted.  If your forms inherit from a base class, you could add that code to your base class and resolve the issue for all your forms in one shot.
private void Form1_Load(object sender, EventArgs e)
{
    SetTextBoxesToMultiline(this.Controls);
}

private void SetTextBoxesToMultiline(Control.ControlCollection controlCollection)
{
    foreach (Control ctrl in controlCollection)
    {
        if (ctrl.Controls.Count > 0)
        {
            SetTextBoxesToMultiline(ctrl.Controls);
        }
        else if (ctrl is TextBox)
        {
            var tb = (TextBox)ctrl;
            tb.Multiline = true;
            tb.KeyPress += new KeyPressEventHandler(this.textboxes_KeyPress);
        }
    }
}

private void textboxes_KeyPress(object sender, KeyPressEventArgs e)
{
    if (e.KeyChar == '\n' || e.KeyChar == '\r')
    {
        e.Handled = true;
    }
}

Open in new window


I hope this helps.
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 37784415
With Multiline true, you'd also have to handle a PASTE then and remove any line breaks.
0
 

Author Comment

by:SimplePaymentSolutions
ID: 37785763
@wdosanjos - Good idea, but it does not take in to account the hundreds of other textboxes that ARE multi-line and need to accept multi-line content.

I guess I could modify that to only change textboxes that are currently NOT multi-line, but that seems awfully clumsy to me. Changing widget properties in mass at runtime is just a bad idea...

I guess I was hoping that someone would know of a more... viable solution. I do not like the idea of changing the textboxes to multi-line even if I were to take the time to do it manually to prevent issues, as doing so makes the performance of the textbox control about 1000x slower. (you can verify this by changing it in the sample I attached and click the button - you will be amazed at how much longer it takes to manipulate it), but that said, it appears that may be the only solution to this... Tho I do want to leave the question active for a while to see if someone knows of any other solutions...

What would be perfect is if someone knew WHY the textbox did what its doing... and knows how to adjust it to some greater threshold to allow larger values before appearing empty. There has to be a logical solution somewhere... I would think. Unless this is just a bug in the textbox widget? That could be I guess lol.

Dave
0
 

Author Closing Comment

by:SimplePaymentSolutions
ID: 37877232
I am accepting this as it is the best that was found - but does not solve most of our issues.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
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…

759 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

20 Experts available now in Live!

Get 1:1 Help Now