3 ms·
There are several ways. The simplest is setting the "Align" property of a control which is similar to the "Dock" property of WinForms in that it makes the contr
by badsectoracula 8y ago
There are several ways. The simplest is setting the "Align" property of a control which is similar to the "Dock" property of WinForms in that it makes the control stick to the left/right/top/bottom edge (multiple controls stack) or fill the "client" area. It also has an "Anchor" property that again works similar to WinForms' property of the same name, allowing anchoring an edge to the location you place the control.
Beyond the above (which i think are all what WinForms provide, at least from the visual designer) the anchors can also be set so that instead of just "sticking" to a designed edge, they are set relative to some other control in the parent container with customizable gaps. For example you can configure (using a dedicated anchor editor window) that the left edge of an input box is 5 pixels away from the right side of the label control and the label control's top edge is aligned so that the label's center matches the edit box's center. Note that any control can be anchored to any other control, so if you have a form with 10 input fields you can anchor their left side to the same control (e.g. the longest label or a "guide" control) so that they align neatly.
The align property is enough for most cases and the anchor system covers almost the rest of the layout cases. Where those do not suffice, there are a few more dedicated options like the TFlowPanel container that flows controls in various formations as well as the ChildSizing property that allows for specifying some automatic layouts. TBH personaly i never had to use those though. Note that most of them can also be combined.
Finally each control also has constraints (min/max size) and spacing (margins) for each edge as well as an overall spacing.
Somewhat related, but you can also make pretty much any control dockable by setting its DragKind property to dock and DockMode property to automatic. Docking happens in "dock sites" which are basically any container (like a panel) that has its DockSite property set to true (and preferably, although not required, its AutoSize also to true) and since containers are regular controls they themselves can be laid out using any of the above options or be anchor targets for sibling controls. The framework handles the whole drag/drop, docksite resizing, creating popup windows for tearoff docks, etc. In fact the whole docking system is modular itself and there are a couple of different docking managers that provide quite advanced options.
Note that all i've mentioned so far are done with the visual designer, there is obviously more fine grained control by writing code.
- int_19h 8y agoWinForms also has layout panels (they showed up in 2.0+) - FlowLayoutPanel etc. The stuff with anchors being relative to other controls, though, sounds like it should be roughly equivalent to Java GroupLayout in expressive power. Which is neat!
- badsectoracula 8y agoI do not think GroupLayout is similar as there aren't really any groups and the anchoring is done directly to the other controls. And TBH from my experience using it in NetBeans i wouldn't describe anything other than a PITA :-P (although half of it was probably NetBeans itself).