Dock, Anchor and Fluid layouts in Windows Forms Applications - c#

So, I've been asked to redesign an old application I wrote a few years ago.
Basically, nothing much needs to be changed, except that the Customer wants it to be more fluid, and that it must be fullscreen (no visible "window") I.e. no Titlebar, just a Borderless fullscreen Window.
What is the best way to make sure everything stays fluid, I mean how can we make sure everything appears where it should, 'cause you know, different resolutions, monitor sizes etc?
This is easy in web pages/css, but this is not something I've done before. Most of the Controls will be created programatically at runtime, based on what action was performed, etc. How would I accomplish such a layout? Basically I want to be able to lay it all out full screen, without knowing how large their monitor is, or what resolution they're using.

Your certainly correct in trying to design your form using a fluid layout that responds to the size of the available space and size of the form font. To do that you want to use the following controls and control properties.
1, TableLayoutPanel will split an area into a set of rows and columns and allow you to position your child controls within individual cells of that table layout. This responds to a change in the form width and height.
2, FlowLayoutPanel will position your child controls from left to right and automatically move to a new row when you run out of space. This is great for a fluid design as it will adjust the layout depending on the available space.
3, Control.Anchor property allows a child control to alter position and size based on the size of the form client area. So you make your control always be a fixed offset from the right or bottom edges.
4, Control.Dock property will position a child control against an edge and the opposite size will automatically be defined by the containing form.

You could put Your controls into tableLayout, and set the Dock property to fill.

Related

How to view inside of a panel control in C# windows form

I am trying to place a panel that I want to have multiple textboxes inside of. I want this panel to fit inside the form and the panel to have a scrollbar. I have autoscroll on and its working as intended, but I want to be able to add controls below the size of the panel. Is there a way I can "full screen" the panel and place items in it or even make the scrollbar a set length instead of just the amount needed to fit the contents. I want to be able to place things underneath what I have there.
Dragging items onto the panel doesn't allow enough room to make things tidy
As an option, you can set the AutoScrollMinSize, in your case, the height of it to a larger value like 1000 temporarily, which sets the virtual (Scrillable) height of the control to 1000. The property determines the minimum size of scrollable area.
As another option, you can AutoScrollMargin, in your case, the height of it, to something like 500 temporarily, which provides an extra 500 pixels space at bottom of the panel. The property determines the minimum margin that should be between the edge of the child control and the edge of the scrollable control.
Then when you are done with the design, just right click on the property and Reset the value of it, to let the control calculate the scroll size.
And I assume you are aware of some obvious workarounds like:
Design the form (including the panel) in a larger size, setting proper dock and and anchor properties for controls, later set the size of form to desired size at run-time, or at design time (after you are done with the initial designs).
Or another workaround, could be just dropping the controls in the panel, and then selecting them and moving them using arrow keys.

How to make C# winform responsive

I want to make responsive windows form app layout that needs to be responsive to any windows size
for example, when we want to reduce windows size all elements become small as well
I have seen that some users said that we can make all elements stick or use methods but I'm looking for an efficient one and easy as well
Like others already mentioned what you're looking for are the control properties Dock and Anchor which define how an objects size and position should change according to it's parent container.
With Control.Dock you can dock an object to an edge of it's parent container and it will remain there even when you resize your form. An example would be a navigation menu that's always docked to one side of your Form.
With Control.Anchor you can anchor one or more edges of an object to it's parent container. That means you can place an object anywhere, anchor it to one or more sides and the defined sides will "stick" to it's parent containers sides when resizing.
What could also be of use for you is the Screen Class (Documentation) for example if you're trying to determine the Screens Resolution and set your forms Size accordingly.

Define a dashboard panel with child resize logic

I am trying to write a XAML UI in WPF where the main window container (a panel) would host children.
The tricky part is I want the children to resize when the window resizes (e.g. when it's being maximized on a screen) and I want them to occupy the maximum of the available space without stretching.
But at the same time I would like them to be allowed to wrap like a wrap panel when possible.
I have designed a quick mockup for easier understanding.
Image mockup on Imgur (25.2KB)
The top one shows a panel hosting 3 elements which all share the same width and the same height. Two are on the first line, the third one takes advantage of the 2nd line to display.
If all were displayed on the first line, they would be of a smaller width.
The bottom one shows a panel hosting 8 elements which all share the same width and the same height (smaller than in the first mockup so they can fit in one screen). The first two lines have 3 elements each while the last line has only 2.
My initial idea was to use a Stackpanel but they strech your child elements and, as far as I know, they don't allow dynamic sizing of their children depending on the number of elements.
Then I had a look at the wrappanel which does the wrapping very nicely but requires you to set the children size in order to do its magic (I might have missed something but I couldn't find a way around it).
Do you have any idea how I could implement this behaviour while keeping my pannel flexible?
The application which is going to use it will have a different number of children to put in the panel depending on the user's settings.
Ah, I forgot to mention that there should obviously be no vertical scrolling, everything should fit on one screen like a dashboard. That's the point of this panel, make sure that everything fits but displays as big as possible, with no distortion.
If you have any question, just ask.
Thank you for your help.

Best Way to make a Windows Forms scalable?

What would be the best way to make a WinForms application fully scalable, for example when the Form resizes?
In WPF i would use something like a Viewbox and/or a UniformGrid, but something like this doesn't exists in WinForms.
Is there an easier (and maybe faster) way to rescale controls on a from after resizing it, instead of resizing them all by calculating their new Size/Location etc.?
Thanks in advance
In Windows Forms, you use the Anchor and Dock properties for each control.
Here's an article about using them: http://www.techrepublic.com/article/manage-winform-controls-using-the-anchor-and-dock-properties/
You should also look at FlowLayoutPanel and TableLayoutPanel
you can use anchor and dock, depending on your need:
Anchor - the edges of the container to which a control is bound and determines how a control is resized with its parent.
Dock - which control borders are docked to its parent control and determines how a control is resized with its parent.
for further read: Dock and Anchor
Have a look at the Anchor property found on pretty much any control. This allows you to lock a control to any (or all) of the four borders of a window.
Once one distance is anchored (e.g. Top or Right), the control will always try to keep that distance, no matter how you resize your window.
For example, you'd set Anchor to Bottom and Right for a button that is supposed to always stay in the bottom right corner of a window. A text box, that should always fill the window from left to right would use Left and Right.
Similar things can be achieved utilizing Dock, but a docked control will always try to fill as much space as possible (there are different strategies available, like "fill everything from here and upwards) based on its container. Depending on your use case, this can however be a lot harder to control (and I usually only use it if I want a single control to fill a full window, e.g. a TextBox).
If you need more complex alignment, like widths scaled on some kind of ratio (e.g. 30%), then there are several different containers available.

Resizing componenets on a c# form

typically in java if you have a layout manager of somesort, when you resize the page then the components in that panel will resize accordingly. I think my app is missing some sort of layout manager to control resizing as at the moment everything is just static
Is there a way to make it resize on the changing of the form size? say the user makes the page bigger, then the componenets adjust and so on.
Thanks
.NET has layout managers as well.
Personally, I prefer the TableLayoutPanel for my WinForms apps.
Once you layout the Table (using a combination of static/dynamic sized rows/columns) you add your child controls to the table cells. Once you add your controls, you can dock or anchor the controls to the cell so that they are automatically adjusted when the window is re-sized.
Two main options:
Anchoring. Set your control to "anchor" to the sides of your form. This means that if the form resizes, the control will stay a constant distance from that side. So, if you anchor Top, Left and Right, then your control will stay in the same position, but resize horizontally with the width of the form. Play with it. It'll be obvious.
Docking. Set your control to "dock" to a side of the form, or the center. This is usually done with containers, and it will make the widget take up that entire portion of the form no matter how large it gets.
In Windows Forms you make use of the Control.Anchor property, which will cause the control to adjust accordingly when the window resizes.
To do this with windows forms you use the Anchor and Dock properties of the control
See this for a guide on how to use them

Categories