1

Controls are in the wrong location when they are anchored to bottom or right in a SplitContainer that's in a Panel or another SplitContainer at high DPI settings like 160 % with .Net Framework 4.0 or 4.5. It happens with the form's AutoScalMode set to either Font or DPI.

It works correctly if the control is anchored top-left or the SplitContainer isn't in another control, or using .Net Framework 3.5, or at 100% DPI.

How can I fix it?

At 100% DPI, it looks correct, like this: Form with button in bottom-right

At 160% DPI, the button is in the wrong place, like this: enter image description here

user1318499
  • 1,327
  • 11
  • 33
  • Maybe https://stackoverflow.com/a/29766847/10216583, and https://stackoverflow.com/a/29766847/10216583 and https://stackoverflow.com/questions/13228185/how-to-configure-an-app-to-run-correctly-on-a-machine-with-a-high-dpi-setting-e?answertab=active#tab-top help. –  Mar 21 '20 at 20:56
  • Do you have the child container which hosts the button `Dock.Fill` ed.? It's a big location offset for that button. I believe the `Anchor` property for that button is already set to `AnchorStyles.Bottom Or AnchorStyles.Right` –  Mar 21 '20 at 21:16
  • JQSOFT, I can't find anything helpful in those articles. There's no `Dock` property in the bottom `SplitterPanel` but the top `SplitterPanel`, `SplitContainer`, and `Panel` all have `Dock.Fill` set. Yes, the button is anchored to `(...Bottom or ...Right)`. – user1318499 Mar 22 '20 at 00:25
  • 2
    Is your app DpiAware? Anyway, as a suggestion, use a TableLayoutPanel to define the layout of child controls. In your case, you can also use another Panel, docked to Bottom, inside the SplitContainer's Panel and add the Button to this other Panel. Nesting containers is a common design feature. – Jimi Mar 22 '20 at 00:48
  • Yes, it has the `dpiAware` setting in the manifest. Most other controls scale properly, including ones in other `SplitContainer`s, just not in the nested `SplitContainer`s. – user1318499 Mar 22 '20 at 02:03
  • I don't see nested SplitContainers there, but make sure that the Parent of that Button is actually what you think it is. Add a Panel to the *right* SplitContainer child Panel, give it a distinct BackColor and then add the Button (cut&paste) to the new Panel. Dock the Panel to Bottom. – Jimi Mar 22 '20 at 02:28
  • @Jimi That nested panel works. I also had to add a right-docked panel inside it to correct both bottom and right alignments. I'm still kind of hoping for a cleaner solution that just reverts to the correct .Net 3.5 behavior somehow. – user1318499 Mar 22 '20 at 06:45

1 Answers1

2

In the SplitterPanel, put a TableLayoutPanel with a single cell and Dock = DockStyle.Fill. In that cell, put a Panel with Dock = DockStyle.Fill, then put the controls in that innermost Panel and the anchoring works correctly.

Another way that's not as convenient for controls that aren't anchored to bottom-right is in the SplitterPanel, put a Panel with Dock = DockStyle.Bottom (red) and inside that, put another Panel with Dock = DockStyle.Right (yellow), then put the controls in that innermost Panel.

Here it is at 100% DPI as designed: Two nested Panels at 100% DPI

and at 160% DPI the button is still in the correct place: Two nested Panels at 160% DPI

user1318499
  • 1,327
  • 11
  • 33