简体   繁体   中英

Anchored controls won't resize when window is resized (56k beware)

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:

替代文字替代文字替代文字替代文字

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.

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.

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.

我遇到的问题是usercontrols没有按照你所描述的方式执行操作,因为usercontrol autoscalemode属性设置为'font' - 尝试将其更改为继承。

尝试在TableLayoutPanel的容器上设置Dock = DockStyle.Fill

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM