Why does Windows Forms docking not work properly when nesting is deep?

Stugol

New member
Joined
Dec 31, 2011
Messages
2
Location
UK
Hi. I hope someone can help me. Ive been scratching my head on this one for weeks, and cant 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) doesnt resize properly when the parent controls change size.

Oh, it doesnt 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. Also, I believe the SizeChanged event doesnt fire.

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.

Ive attached the source and the executable separately, for those of you who distrust executables. The source consists of a VS2010 project and solution, along with half a dozen usercontrols that contain no code.

Source: View attachment 5
Executable: View attachment 6

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 set 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. Note 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 isnt actually limited to multiline textboxes, but Ive only been able to reproduce it with them in this case.

Id really appreciate some help here guys. Nothing I try seems to help. Ive tried adding calls to PerformLayout and such, and they dont do anything. (Worryingly, the Microsoft source for the layout code contains comments along the lines of "this bit shouldnt be needed, but if I take it out, things break". I thought Microsoft were supposed to understand this stuff?)

Please dont suggest silly things like "dont use docking" or "dont nest your controls so deep" - they arent 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 shouldnt have to do anyway), I have no control over how deep anyone else chooses to nest my control; and supposing I subsequently need to use my control inside another control? Nesting should work. Docking should work.
 

Attachments

I have solved this problem. However, since nobody here even tried to help, Im keeping it to myself.
 
Back
Top