space winforms controls - c#

I have a simple winforms app with a column of several buttons. What is the nicest way to enforce that they are equally spaced vertically?

Drag a rectangle around them in the designer so they all get selected. Use Ctrl + Left mouse click to adjust the selection if necessary. In the menu: Format + Vertical Space + Make Equal.

If you want to make the spacing dynamic (ie: it updates when the user changes the size of the window) the best approach would be to create a TableLayoutPanel with 5 rows (and probably just one column). Set its Dock property to Fill, and make sure each row has automatic size.
After that, you place your buttons on the panel, and it will do the positioning for you.

Set the top and bottom margins on each control to the same thing, and then drag them together using snaplines to line them all up.

Change the location parameters in the properties windows to be proportionally aligned.

Related

Win Forms Dock fill with gap

Fore note: I know you cannot use a margin with dock, but I am trying to figure a way around this.
I have two objects, a GroupBox (containing loads of buttons and stuff that will always be the same size no matter how big / small the form) and a WebBrowser. The former will take up roughly 100 pixels at the top, and the latter will take up the rest of the space. I have tried multiple ways to get around this, including Panels, GroupBoxs, changing Anchors and Docks, but nothing is working. I know there is a simple solution for this, but I cannot work it out. Could someone point me in the right direction for what I should be using?
P.S. New to WinForms so not very knowledgeable of things.
Start with a TableLayoutPanel control on your Form and set its Dock() property to Fill. Now change the ColumnCount() property to 1, and leave the RowCount() property at 2.
Add your GroupBox to the Top Row and adjust its size. Add your WebBrowser control to the Bottom Row and set its Dock() property to Fill.
Finally, select the TableLayoutPanel, find the Rows() property, and click on the "..." dots to the right. Select Row1 and change its Size Type to AutoSize.
Done!
Alternative Approach...
Add your GroupBox to the Form and set its Dock() property to Top. Add your WebBrowser control and set its Dock() property to Fill. Note with this approach, however, that the GroupBox will extend to fill the full width of the Form.

Is there a way to make vertical spacing equal programmatically for button controls?

I know there is a way to make spacing equal for controls in the designer view. But is this possible to do it programatically? Because I have a form with multiple buttons and we dont know which buttons will be displayed and which will be hidden at the compile time. So they look uneven when the form is displayed(some with more spacing and some with less spacing). Is there a way to re position them at the run time? Is there an equivalent to doing the Format -> Horizontal Spacing -> Make equal programatically?
Thank you for any help.
You could achieve it using FlowLayoutPanel. Which represents a panel that dynamically lays out its contents horizontally or vertically.
When all the buttons are visible
When button3 is hidden at runtime

how to keep objects in place when window is resized in C#

How can I keep the objects of my window (buttons, labels, etc) in center when the window is resized?
Currently, I have 3 buttons in a Windows Form. When I maximize the window, the buttons stay at the top left section of the window. I want them to be in the center as they were when the window was not maximized.
Any help?
You should set the Anchor properties of the object to none,
This will keep it in the middle.
Set the Anchor property of your controls correctly. By default your control is anchored to Top,Left. If you clear this property (anchor to nothing, essentially), your button will remain centered.
(It may seem like you want to anchor to all four sides, but in reality what this will do is resize your button to fill the form!)
To keep your layout fixed and in the middle do this:
On your Form add TableLayoutPanel.
Set it's Dock property to Fill.
Create 3 rows and 3 columns.
Edit rows and columns - set 50% for first and last column and row.
Set fixed size for middle row and column.
Place Panel or anything else you like in 2nd row and 2nd column. It will always be in the middle.
If you are using the visual designer of Visual Studio (And you have no reason not to), the property of your control you seek to manage how they are placed inside a form is "Anchor".
By default, when you create a new control, it is set to "Top-Left", which mean they would stay in a fixed position to the top left of your form. You can change that to anchor them to anything.
You can also disable the anchors and control their position by overriding the Resize method of the form.

User Interface Layout in c#

Kinda new in Microsoft visual C#. I have made a simple program. Everything is working perfectly fine, unfortunately the tricky part for me is i do not know how to layout my objects properly. I would like to copy the layout of the Microsoft visual C# interface, wherein the panels adjust to their predefined ratio and proportion whenever the main form is re-sized and the user may adjust the width and height of each panel. Any readings or code would be a lot of Help. THANKS A LOT!
You could do a number of things:
Allow automatic layout using something like a FlowLayoutPanel
Allow resizing of controls using a Splitter
Look at custom implementations to provide more advanced functionality Collapsible Splitter
Well follow these tutorial links to know about resizing in windows
using Dock and Anchor property. Along this the layout control will
help you to manage the layout - FlowLayoutPanel and
TableLayoutPanel, Panel, GroupBox etc.
Designing Resizable Windows Forms in Visual Studio .NET
Manage WinForm controls using the Anchor and Dock properties
This one is much better to understand.
For a simple start the anchor property is what you want. so for instance if you set all four anchors for that left hand control, and the parent window changes size it will will resize proportionally.
After that it starts getting complicated. Adding panels and then putting your controls inside them. Setting Dock to left, or top or fill. Grow and shink on scrollable controls. Splitter bars.
And last ditch handling resize events and calculating positions and sizes.
Sit down and have a think about what you want to happen, play around with minimum and maximum height and widths, ie no point in working out waht your form is going to look like when it's postage stamp sized...
PS Don't forget VS allows floating panels, and persists (well some times sort of) user choices in the layout, that's a bigger job.
Place a TableLayoutPanel as the base control, anchor it to all sides, define as many columns and rows as you like with "percentage" sizes.
Then place different sections of your form in different table cells. Properly dock your controls in each cell.
Can also use a split container above table if needed.
The ideal order should be like below
SplitContainer
TableLayoutPanel
Panel
Controls

Docking and Anchoring on a Windows Form application

I'm developing an app for Windows Mobile 5.0 and above, with C# and .NET Compact Framework 2.0 SP2.
I have a WinForm with two panels inside (upperPanel and bottomPanel). I want that upperPanel always fill 2/3 of form's height, and bottomPanel fills 1/3 of form's height. Both panels will fill completly form's width.
I've used this:
upperPanel.Dock = Fill;
bottomPanel.Dock = Bottom;
But upperPanel fills the form completly.
How can I do this? I want, more o less, the same gui on differents form factors and on landscape or protrait mode.
Thank you.
What you need to do is to put the bottom panel on first and set its Dock property to Bottom. Then set the panel's height to be 1/3 of the form's height. Finally, add a second panel and set its Dock property to Fill. The key here is that you want to add the control that will fill the remaining area to be added last. Alternatively, you can play around with the Bring to Front and Send to Back commands in Visual Studio to get the designer to cooperate.
You may also need to hook the OnSizeChanged event for the form and re-set the height of the bottom panel to account for layout changes. It's been a little while since I did compact framework programming, so I'm not sure.
Right click on the upperPanel and select Bring To Front. However, I don't think this will give you the result you want. When you resize, the bottom panel will remain the same height, while the upper panel will stretch to fill the form.
Using your docking settings, with this code might do the trick:
protected override void OnSizeChanged(EventArgs e)
{
base.OnSizeChanged(e);
this.bottomPanel.Height = Convert.ToInt32((double)this.Height / 3.0);
}
Set both panels to "not anchored". That is: Remove Dock-Value and clear the Anchor property. Then, move the controls so they are sized the way you'd like them to be sized.
After that, upon resizing the form, they should resize relatively.
EDIT
Oops, just tried it and sure it doesn't work. I mixed this up with a solution that automatically keeps controls centered within the window...
Well, I'd guess you then have to create a handler for the form's Resize event and manually align the controls after the form has been resized.
Go to Tools, Other Windows, Document Outline. Find the two panels, and swap the order of them. The control that has DockStyle.Fill has to come first for it to be docked correctly. (or last.. never sure which one it is, but it is one of them :p)
This won't solve the always 1/3 and 2/3 issue though... cause the bottom panel will have a fixed height (unless I am mistaken). I think maybe the TableLayoutPanel supports this though...
Update: As noted in the comments, that panel doesn't exist in the compact framework. So, I suppose the easiest solution to this problem would then try to use the docking, but update the height of the bottom panel whenever the size of the form changes.
If you want this to work perfectly you'll need to add some code to the Resize event of the Form which then specifically works out the relative sizes and places the controls in the correct place after a resize.
If you're not worried about losing precision and the forms aren't going to move much you can avoid this by using some relatively smart anchoring. Essentially you're going to have to select a "grower" (the part of the form that gets bigger, the bigger the form gets). In this scenario I would probably anchor the top part to Top | Left | Right and the bottom part to Top | Left | Right | Bottom. This would mean that the lower part of the form will get bigger if the form is expanded. In most cases this is acceptable. If it isn't use the Resize event and some code.
The easiest way to do this is to nest panels. Just set up panels for top bottom and fill. Then use panels within those panels to do the same. The only issues I've had therein are datagrid resizing, which is always a pain anyway. in that case, you have to use some code to resize the datagrid control on the form resize event.
I would like to add a point to #jasonh answer.
For the panel that occupies 2/3 of the form, you will have to set the AutoScroll property of the panel to true.
This will enable the panel to display scroll when the control size exceed the visibility to the user and also ensure the visibility of the smaller panel which is 1/3 of the forms height.
You can get the required design by using nested panels along with few setting with Anchoring and Docking Properties.Follow the following steps:
1) Add the Form and put a Panel1 on it. Set its Dock Property as 'Fill' and ResizeMode as 'Grow&Shrink'.
2) Add Second panel2 and set its Dock Property to 'Bottom', Set the Height and set the Anchor property to 'Top,Left'.
3)Add Third panel and set its Dock Property to 'None', Set the Height and set the Anchor property to 'Top,Bottom,Left,Right'.
Save and Compile. Now all the panels Would maintain their relative Positioning With resizing.

Categories

Resources