there seems to be a bug regarding AutoSize and anchors in TextBoxes. The text box in the screenshot has Top, Left and Right anchor:
As you can see, the box grows vertically, but the text inside does not wrap and actually use the space. I believe it should not be growing in any direction if WordWrap is not enabled. However, enabling WordWrap does not work either, the text stays single-line, which might be related or a second bug. When I enable WordWrap, the vertical growing of the TextBox makes sense, but the text does not wrap.
I attached a repro project.
I could reproduce the problem as described below
(…) when you enter a long text into the textbox and then resize the window horizontally. The TextBox will grow and shrink vertically even though the text stays the same.
It also happens with ComboBox. Logged as WJ-9147
yes, I know that the Anchor is Top, Left and Right. I believe I need it like this for my layout to work correctly.
About the sample project: If you clear the text in the TextBox, it looks like this:
That’s correct and how I would expect it to look – it has the height of one line of text. Now if I start typing some text into it, it will at some point grow vertically as if to fit the (word-wrapped) text:
If I continue to type text into it, it will grow even larger:
I just noticed that this sometimes does not happen immediately. Sometimes you have to re-open the designer for the height to adjust, which is also a bit strange.
My guess is that the TextBox grows so that – if the long line would have been word-wrapped – it would fit into the box. But the text does not wrap. It doesn’t matter whether I set “WordWrap” to true or false. I thought the point of the “WordWrap” property is to wrap long lines so that you can see it all at once without having to scroll inside the TextBox, and the “Multiline” property is just to allow/disallow entering new-lines into the TextBox, e.g. by pressing Enter.
How else would I be able to disallow new-lines in the text but at the same time wrap long lines? I mean, word wrapping does not insert new-lines (i.e. it does not actually make the “Text” property multiple lines), it’s just a visual thing, so I believe “Multiline” should have no effect on it.
To give a bit more context: We are using this kind of layout in a questionnaire. It’s basically a TableLayoutPanel with 2 columns (the question on the left, and a TextBox for the answer on the right), and as many rows as there are questions. Now if a user needs more space for his answer, I would like the row of that question/answer pair to grow as soon as the user’s text doesn’t fit anymore. It should not be possible for the user to enter multiple lines, but the text should wrap nonetheless. I could of course filter out new-lines after the fact, but that’s more of a workaround than a fix for the problem.
You can actually see the problem much better when you enter a long text into the textbox and then resize the window horizontally. The TextBox will grow and shrink vertically even though the text stays the same.
In the sample you sent, the textBox1 is Anchor Top, Left and Right. So it grows or shrinks horizontally, when the windows grows or shrinks horizontally. That’s the standard behaviour. If you don’t want it to grow or shrink horizontally, you can’t Anchor it both Left and Right.
I don’t understand what you mean by “enabling WordWrap”. If you want textbox1 to show text in several lines you must
this.textBox1.Multiline = true;
just like as in WinForms.
Please login first to submit.