Hi. I hope someone can help me. I've been scratching my head on this one for weeks, and can't seem to get anywhere.
The problem is this. When you nest controls inside other controls and usercontrols - say, six or seven levels deep - the inner control (which in this particular case is a multiline textbox) doesn't resize properly when the parent controls change size.
Oh, it doesn't just fail to change size. That would be simple. No, its bounds change, and in some (but not all) cases it repaints itself properly, but
the text continues to wrap at the same place, even though the control size has changed.
I first encountered this problem when I was writing a custom panel control, and thought it was just a bug in my code. After banging my head against a wall for a while and getting nowhere, yesterday I decided to take a copy of my code and remove bits until the problem went away.
I removed all the code. The problem remained.
The only code remaining is the GUI code that Visual Studio puts in behind the scenes when you add controls to the design surface, and a tiny amount of extra code to colour the various controls so I can see what is going on.
Now, I can't very well paste the Windows Forms Designer code in here - it's quite long-winded, as such code tends to be. Plus, there are half a dozen different classes (all pretty much empty). So what I've done is, I've uploaded the source and the executable separately to MegaUpload:
Source:
http://www.megaupload.com/?d=LDP09KRY[
^]
Executable:
http://www.megaupload.com/?d=P7QKDMJJ[
^]
If you don't trust executables, just download the source. It consists of a VS2010 project and solution, along with half a dozen usercontrols that
contain no code.
To reproduce the problem, run the program and observe the three sets of concentric squares. Each is a panel, tablelayoutpanel, or usercontrol. In the centre of each is a textbox. Make the window wider, and notice that the text in the top textbox
does not re-wrap to fit the new width of the textbox.
If the textbox itself appears not to resize, it has merely failed to repaint itself. Click on one of the other textboxes, then click on the offending textbox, and it will repaint. Notice that the text will still be wrapped incorrectly.
It is interesting to note that if I simply nest controls instead of using (codeless) custom controls; or if I use a single-line textbox or, say, a button; the problem goes away. As far as I recall, the problem isn't actually
limited to multiline textboxes, but I've only been able to reproduce it with them in this case.
I'd really appreciate some help here guys. Nothing I try seems to help. I've tried adding calls to PerformLayout and such, and they don't do anything.
(Worryingly, the Microsoft source for the layout code contains comments along the lines of "this bit shouldn't be needed, but if I take it out, things break". I thought Microsoft were supposed to understand this stuff?)
Please don't suggest silly things like "don't use docking" or "don't nest your controls so deep" - they aren't helpful. Reason being, docking is the
official method of arranging controls in .NET; and even if I felt able to reduce the nesting level (which I shouldn't have to do anyway), I have no control over how deep anyone else chooses to nest my control. Nesting should work. Docking should work.