2

I'm running into a strange issue with a WinForms textbox. When I set the Text property to a long string, the text seems to vanish. My understanding is that a textbox in Winforms has a default MaxLength of 32767, which can be set to any value less than or equal to int.MaxValue. The steps to reproduce the issue are as follows:

  1. Fire up Visual Studio 2013
  2. Create a new Windows Form Application. Choose to target the .NET 3.5 framework.
  3. Drag a textbox control onto the form.
  4. In the form load event, type in the following code:

    private void Form1_Load(object sender, EventArgs e)
    {
        // Setting the MaxLength property should be unnecessary since the
        // default is 32767, but I'm implicitly setting it anyway.
        textBox1.MaxLength = int.MaxValue;
        string s = "";
    
        // Weird things happen when the value in the next line
        // is set to anything >= 4680
        for (int i = 0; i < 15000; i++)
        {
            s = s + "A";
        }
    
        textBox1.Text = s;
    }
    

Run this application and you'll that the textbox "appears" empty. I say "appears" because if you put your cursor in the textbox, you can see that it behaves as though something is in the textbox, but nothing is there visually.

Whatever is going on, 4680 seems to be the "magic number". If you change the number in the for loop to 4680, you still get no text in the textbox (although clicking in the textbox will make the text show up). If you change it to 4679 or anything smaller, then it works just fine. Also, changing the Multiline property to true makes it work just fine.

Does anyone have any ideas or workarounds for this odd behavior? I guess my workaround could be setting the Multiline property to true, but I'm still intrigued by what causes this behavior.

Icemanind
  • 47,519
  • 50
  • 171
  • 296
  • can you post an example of this `Long String` do you have autosize property set= true..? do you have wrap text set = true.. what is the height and length of the textbox.. use a richtext box – MethodMan May 08 '15 at 19:11
  • Why did someone downvote my question? It's written clear and contains step to reproduce the issue. – Icemanind May 08 '15 at 19:12
  • @MethodMan - I posted an example of how to reproduce the issue. I'm not changing any of the default properties on the textbox. – Icemanind May 08 '15 at 19:12
  • 1
    [Another question](http://stackoverflow.com/q/17476973/62576), while in a different language, confirms that this is a known issue with the Windows Edit control, which is what is beneath both the WinForms `TextBox` and the Delphi `TEdit` at the API level (as well as the multi-line versions of those controls, which in Delphi is named `TMemo`). – Ken White May 08 '15 at 19:27
  • @KenWhite - That question you linked certainly does seem to confirm a bug in the underlying Windows edit control. 4680 seems like such an odd number though for it to stop working. Oh well – Icemanind May 08 '15 at 19:34
  • Yes, it does. Not sure why that would be the value. The workaround seems pretty clear, though; don't allow your users to enter more than 4680 characters in a single-line edit control (which seems to be pretty logical anyway - they can't possibly read text that needs to scroll horizontally that far), and don't put text that long into a single line textbox via your code either. :-) – Ken White May 08 '15 at 19:55
  • 4680 = 0x1248 ... not as odd but probably arbitrary. In Octal 11110 – Steve May 08 '15 at 19:59
  • @KenWhite - I know it seems odd to have a textbox that size in the first place. I am working on a program and in the program, SQL database tables can be selected. The selected tables are then parsed into a string, such as `"table_1;table_2;table_3;table_4;table_5"` and that string is what's being put into the textbox. I should rethink the UI on that a bit. – Icemanind May 08 '15 at 20:15
  • I just played in LINQPad to test some stuff, but I noticed that the cutoff was with 4096 being the first length of string where the `TextBox` would no longer display its Text. I was creating the string via `new string('A', charCount)` for simplicity. – Anthony May 08 '15 at 21:01
  • 1
    @TaW I just used `TextRenderer` to measure the width of `new string('A', 4096)` [32775px] and `new string('A', 4095')` [32767 == `short.MaxValue`], which agrees with your linked answer. Icemanind's number of 4680 is likely just a font difference. – Anthony May 08 '15 at 21:16
  • Yes. The problem could be 'solved' by using a smaller font, of, maybe 1pt or less ;-) - It should be noted, that only the __rendering__ is affected. The __Text property__ holds the full length of the string! – TaW May 08 '15 at 21:20

0 Answers0