Click here to Skip to main content
15,898,773 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
Posted
Updated 20-Jun-17 13:20pm
Comments
Sergey Alexandrovich Kryukov 31-Dec-11 18:18pm    
You are right. You really need to use Dock and nest as deep as you want; and good nesting is a good sign. Nevertheless, the layout is easy to screw up. The problem is probably not in UI code but it Designer -- it sometime "helps" to screw up things...
--SA
ChewsHumans 31-Dec-11 18:43pm    
Well, I've looked until my eyes bleed, and found nothing. I suppose you couldn't have a glance at the project and see if anything occurs to you? I'd appreciate it.
Sergey Alexandrovich Kryukov 31-Dec-11 23:44pm    
I don't have VB.NET installed, sorry. This is the first thing I remove from installation options when installing Visual Studio.

I put some advice in my answer, please see.

Good luck, Happy New Year.
--SA

This sounds like a problem I have just fixed. The kernel stack is too small. Its a limitation of Windows. It happens faster in x64 Windows than x86, since the address is twice the size, but the kernel stack remained the same.

For more info (and a workaround):

http://support.microsoft.com/kb/953934
 
Share this answer
 
Comments
ChewsHumans 3-Jan-12 14:11pm    
That worked! Many thanks: I've been tearing my hair out over this one. One question though. Shouldn't you provide a hotfix or something? Like, modify the Control.OnSizeChanged function in the framework to, for example, keep a count of the recursion and use BeginInvoke() automatically when the limit is reached? Because doing it for a single control in the heirarchy will only help for a specific application. When I write another application, I will have to do it again, because the nesting will be different. I might not be using panels, for example, or the panels might be close to the surface or really deep in.
mjhetze 3-Jan-12 15:55pm    
Sorry, I wasn't clear -- I found the error and fixed it my app; I don't work for Microsoft. There is a MSDN blog entry that discusses it in more detail.

http://blogs.msdn.com/b/alejacma/archive/2008/11/20/controls-won-t-get-resized-once-the-nesting-hierarchy-of-windows-exceeds-a-certain-depth-x64.aspx?PageIndex=2

It would be nice if they pushed out a fix for it somehow.

ChewsHumans 3-Jan-12 20:47pm    
Ah, sorry, I misunderstood. Thanks for your help.
Sorry I cannot load your project right now. I can give you some ideas.

My approach is different. There is a certain limit of complexity where using Designer becomes dangerous. This limit is pretty low. For more complex layouts, most of the works should be done in code. Designer is always manual work and no reuse. If you look at auto-generated code, it is not as readable as you can make on your own.

For example, how to solve this problem: "make uniform padding everywhere, and the thickess of padding is defined in one place"? Only in code. Designer violates the Don't Repeat Yourself (DRY) principle (http://en.wikipedia.org/wiki/Don%27t_repeat_yourself[^]); so, it cannot be legitimately used in complex and correct UI. With designer, you cannot really see the layout. Controls cover each other; a control with DockStyle.None can be aligned as docked and looks like docked. Basically, you need to keep all controls docked.

By the way, a simple idea which can help sometimes: color all controls with different background colors. You might be able to see the problem which you don't see right now.

I often use one simple schema: with Designer, I only add status bar, main menu and one main panel in the center, filling the space, than I add the panels in this main panels. Everything else is done in code.

—SA
 
Share this answer
 
Comments
ChewsHumans 1-Jan-12 10:38am    
Yes, you make valid points. However, I don't believe the designer is to blame for this issue. I think there is a bug in the framework layout code itself.

Incidentally, the demo (if you would care to take a look - I provided an executable) already has colour-coded backgrounds. Granted there's a lot of duplication of colours, but I arranged them to avoid confusion.
Well, here is an opensource implementation of nested docking. Try downloading this and Should this similar implementation help you figure out your issue, http://sourceforge.net/projects/dockpanelsuite/[^]
 
Share this answer
 
v2
Comments
ChewsHumans 1-Jan-12 10:41am    
While that is an interesting project - and one which I may use in my own applications - it's purpose is not to replace the docking functionality in Windows Forms, but rather to allow panels to be docked, tabbed and floated in a similar manner to Visual Studio tool windows. Useful yes, but nothing to do with the problem I'm having. Thanks for suggesting it - as I say, I may find it useful, but it doesn't help with my problem.
goto WINDOW menu and click RESET WINDOW LAYOUT..
this is certain solition..
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900