Link to home
Start Free TrialLog in
Avatar of SimplePaymentSolutions
SimplePaymentSolutions

asked on

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

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.
Avatar of SimplePaymentSolutions
SimplePaymentSolutions

ASKER

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.
Avatar of Mike Tomlinson
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?
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
ASKER CERTIFIED SOLUTION
Avatar of wdosanjos
wdosanjos
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
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.
With Multiline true, you'd also have to handle a PASTE then and remove any line breaks.
@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
I am accepting this as it is the best that was found - but does not solve most of our issues.