7

This one's really kicking my backside. I have a form, spawned and owned by the main form of the application, that is used for searching records. The form is laid out in a docked TableLayoutPanel, with a combination of Absolute and Percentage-sized rows/columns so that my labels stay the same size while the data entry and results controls grow. We're talking about 20 controls all told.

The problem is that, although the TableLayoutPanel is fill-docked, and all child controls are also fill-docked, nothing is resizing inside the form when I grab the window edge and drag. Everything resizes just fine in the designer, but not in the actual app.

I did use my Google-fu, and found this SO question which pointed me to this MSKB article. I created a derived FlowLayoutPanel and a derived TableLayoutPanel with the threaded calls and put them in, but it's still not working. This is the ONLY form on which this is happening, and another form has some pretty deep nesting as well (it uses a TabControl and TableLayoutPanel to layout the data entry controls, but no RBs).

Other pertinent info:

  • There are no MaxSize properties set.
  • Nothing is anchored in this form except for the default Top-Left; pretty much everything is fill-docked to its container. However, a child user control containing the search results table is laid out internally using anchoring; this hasn't been a problem in the other form in which I use these controls, where I've fill-docked them to SplitPanels (nested two deep, no less).
  • RadioButtons and Labels are set to AutoSize, but nothing else is. This seems to be the default behavior for these controls even though the values are bolded in the Designer, and matches the setup of other windows that resize properly.
  • Resize events are fired for the form, but not the TLP.
  • The behavior does not change when the TLP is anchored to the window INSTEAD of being Docked (it's never both at once).

I'm tearing my hair out here. Help!

Edit: Here are some of the requested pictures showing layout behavior in the designer and in-app:

alt text alt text alt text alt text

The controls that begin with "Nested" derive directly from the built-in panel controls, and their only change is an override of OnSizeChanged() to call the base method asynchronously (the workaround from the KB article). It doesn't work with the built-in panels either, as I said before. As you can see from the last two windows, the mainLayout TLP simply does not grow even though it's docked to the window in the designer.

Community
  • 1
  • 1
KeithS
  • 70,210
  • 21
  • 112
  • 164

5 Answers5

10

Epic Facepalm.

In the constructor of this form (user code side), I called SuspendLayout() to perform some additional setup that may affect the layout. Guess what I DIDN'T call when I was done.

If you call SuspendLayout to do your own custom layout changes, always be sure to call ResumeLayout(true) (or ResumeLayout(false) followed by PerformLayout()) when you're done.

KeithS
  • 70,210
  • 21
  • 112
  • 164
5

Are the controls anchored (.Anchor property), or are they docked? I have found that setting one disables the other. I would look in the .Designer.cs file and see if both dock and anchor properties are being set, and maybe even remove the proprties from the source (back up beforehand, of course).

Another thing I usually try is to recreate the form in a separate, new form and see if I get the same results.

Gabriel Magana
  • 4,338
  • 24
  • 23
  • See my edit: All controls that are instantiated for this page are docked, however the child controls of a UserControl used on the form are anchored. Mixing them this way wasn't a problem in the main form. – KeithS Nov 12 '10 at 16:33
  • @KeithS: Ok, I meant the _same_ control having both an anchor and a dock property set usually does not work. Can you do something to determine which control is not resizing? For example, setting a background color in a container or something similar that will tell you at runtime which container is resizing and which is not? – Gabriel Magana Nov 12 '10 at 16:37
  • @gmagna: I get ya; no, none of the controls have both a dock and an anchor property set. I'll post printscreens once I sanitize them (can't show the entire world the search results), and I'll try the background color trick. – KeithS Nov 12 '10 at 16:41
  • @KeithS: Also, what happens when you make the form smaller than the content? When you make it larger it doesnot resize, but when you make it smaller what happens? Do you get scroll bars, or is it simply cut off? I went through a global "form cleansing" recently in a large project, so I feel your pain. – Gabriel Magana Nov 12 '10 at 16:43
  • @The initial form size is set to its minimum size, however if I resize the form to be bigger than the set minimum in design, the controls won't shrink when I make the window smaller in-app. – KeithS Nov 12 '10 at 16:50
  • @KeithS: Interesting... try the background color trick, you need to find out at runtime which container is not resizing. Maybe the table layout control is setting up a "shadow" container for itself that does not have the docked attribute set. Otherwise you should set up a handler for the form's resize event and set a conditional breakpoint (triggered when the form grows to over X pixels wide/tall) and then while on the breakpoint see which container stays at the same size and which one did grow/stretch as expected. – Gabriel Magana Nov 12 '10 at 16:54
  • @gmagana: Good instinct, but no dice so far. I set the conditional breakpoint in a blank ResizeEnd event handler on the form (which should fire after EVERYTHING has resized), and looking at the Controls tree, there is only one control owned directly by the form (the TLP) and it didn't resize. I'll try a breakpoint on a ResizeEnd handler for the TLP and see if it's even fired. – KeithS Nov 12 '10 at 17:23
  • FWIW, the Resize event of the TLP was not even fired. – KeithS Nov 12 '10 at 17:27
  • @KeithS: Weird... Can you doa quick test to add a new form and a dummy NestedTableLayoutPanel to see if that in turn resizes ok when docked? If it does, I would look at the Designer.cs file to see what exact properties are set differently. I would also look at the properties in the containing form to see if anything is amiss. When I look at these problems that could be related to the designers I always look at the source code because the designer itself may be hiding something (ie, a property set twice, to different values each time, for example). I think you are close to the solution. – Gabriel Magana Nov 12 '10 at 17:40
  • @gmagana: I created a form that has only a NestedTLP on it, and set the cell borders to something I could see. I then fill-docked it and set it to open instead of the one I had. It worked perfectly, resizing on command. So, I'll dig into the designer and see if there's anything set on the real TLP that wasn't set on the test one. – KeithS Nov 12 '10 at 17:49
1

I've had issues with usercontrols not doing what i expect in the way your describing it might be something to do with the usercontrol autoscalemode property set to 'font' -Try Changing it to inherit.

Hath
  • 12,606
  • 7
  • 36
  • 38
  • if I told you there wasn't an AutoScaleMode to set on the instance of the user control, would you say that was a problem? – KeithS Nov 12 '10 at 16:52
1

Try setting Dock = DockStyle.Fill on the container of the TableLayoutPanel.

Rob Hruska
  • 118,520
  • 32
  • 167
  • 192
0

Do not dock or anchor individual controls inside a TableLayoutPanel. That rarely does what you want.

Joshua
  • 40,822
  • 8
  • 72
  • 132
  • Do not set DockStyle = Fill for them either. – Joshua Nov 12 '10 at 17:48
  • More info needed; what I want is for the controls to take up as much space as they are given by the cell(s) in the TLP, and to have them stretch and shrink with the form. That means I either dock them, anchor them, or roll my own resize logic in the form's Resize event handler. Filling out a fill-docked TLP with fill-docked controls and using the TLP to govern which controls can stretch/shrink, and how, worked perfectly in another form I created for the same app; why shouldn't it work here? – KeithS Nov 12 '10 at 18:00
  • I found the most effective way is to lose the TableLayoutPanel, create all the controls directly on the form, and use Anchors to define how to behave when the user stretches the form. – Joshua Nov 12 '10 at 23:18