C# positioning buttons - c#

I have 3 buttons in my form. What I need to do is when I make the actual form bigger or smaller, the buttons should change their position and size so they look good, so they wouldn't remain the same size and position. I tried to use the anchors, but that does not work very well. What can I use to solve my problem?

You can check dock and anchor properties
http://www.youtube.com/watch?v=afsx1IJULLI
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.dock(v=vs.110).aspx

You should set both left and right, or top and bottom anchors to resize control. If you'll set only one anchor from these pairs, then control will be positioned instead of resizing.
Docking will resize control, because it is equivalent of setting three or more anchors.

Try using TableLayoutPanel, put your buttons inside the columns of the table

Look good is different all the time. I like placing buttons in StackPanel and setting AutoSize property to true. This fixes two issues:
If user has 150% font in Windows settings - your UI does not break;
if you resize window to be very small - your buttons do not enforce minimal width/height and adapt to ratio user has chosen

Related

Make DataGridView stretch to middle of WinForms form?

I have a WinForm form that has two DataGridView controls paced on it such that they are stacked, one above the other against the right hand side of the form.
I would like a way of setting them so that when I expand the form, they expand height-wise with it, as well as width-wise. I managed width-wise by anchoring them to the left and right sides and anchoring the top one to the top and the bottom one to the bottom. However, from here I'm not sure how to get them to use up the space in the middle that appears when the form maximizes...Maybe an image will make my meaning clearer:
Normal Size:
Maxmized; I'd like the grids to expand to take up the full height of the form between the two of them as the red arrows show:
If this question is blindingly obvious I apologise and can only say I didn't really know how to phrase it properly and so found searching for it on Google unhelpful!
You have two options:
TableLayoutPanel or
SplitContainer
The former lets you create a table of many columns and/or rows with various sizing options from absolute and percent to autosize. This is very powerful for layout; but in other respects TLPs are somewhat restricted as the 'cells' are only virtual..
A SplitContainer offers only two panes but lets you treat each with all the things you can do to a container: add one or more controls, anchor or dock them, give each pane a BackColor and make use of its event model.
So if you need just two controls of equal size that adapt to the form size like you showed in the question, a SplitContainer is maybe the better option.
Set the splitter to fixed and make it smaller, anchor the SplitContainer to all sides and drop the DGVs into their panes and Dock them to Fill.
You could also make the splitter moveable to allow the user to resize the panes; if you do that do make the splitter width larger..
Also make sure that the FixedPanel is set to None so that height changes are shared.
Hint: If you want a few more panes to share the space you can nest several SplitContainers.. But for larger numbers do consider switching to TLP!

Stretching controls to take full screen

I am making a video game. I need that it will be available at full screen mode, and with different resolutions.
I already know how to make application maximized and how to remove the built-in user interface, that is not the problem.
The problem is, that when I run it in full mode, all controls simply take the top-left corner, leaving the remaining space empty.
What I want, that controls would be spread evenly across the screen, with new coordinates and sizes, but same proportions.
I tried using anchoring. It works, but only when there is just one control. When there are more controls, and in my menu bar there are 12, and I try using anchoring, they are stretched, but put on top of each other.
Is there a way to get the right result? If so, can you please help me?
Thank you in advance,
Evgenie
Use TableLayoutPanel, anchor it to the left, right and bottom of the form, set ColumnCount=3, RowCount=1, edit column sizes in the Columns property, then drop buttons into corresponding cells and align them using their Dock, Anchor and Padding properties.
There is 'Dock' property for controls. You can add controls to a TabaleLayoutPanel and set the Dock property to DockStyle.Fill.
some ways i remember through which you can achieve it
Dock - MSDN link
Anchor - MSDN link
programitically setting the location according to the screen resolution ration

How to deal with controls and form's stretching in WinForms

Suppose that I have the following form in Designer:
I want to give users the ability to stretch this form as they want and all controls should be located like in the picture, no matter how user changed the size of this form, so they should take the same amount of space and stick to the same controls and borders.
How can I do it in WinForms? I know that there are such things like Docks etc, but I didn't find the correct way to use them in this situation.
You want the Anchor property in this case, not Dock. Anchoring means that a control will always keep the same distance to certain sides (top, left, right, and/or bottom) even if it means that the size must be changed; docking OTOH does not care about margins, it just fills up all available space on one or all sides.
Here's what you might want to do:
Anchor the two image buttons to the top and right.
Anchor the OK button to the right and bottom (I guess).
Anchor the large ListBox to all sides.
Just To Add some notes on good answer of stakx
For Controls Like ListBox that have a limit to their height, setting anchor is not enough and you should set IntegralHeight of them to false.
I reccomend to set MinimumSize of Form to prevent a user from sizing a window to an undesirable size.In your case Set it to a minimum acceptable size to prevent ugly small form with an unusable ListBox.

How to enable components to resize according to the change in the form size?

I made a simple button based form for a particular resolution, say, 800*480.
I want the buttons to automatically resize themselves when used on a higher resolution.
I have six buttons of equal size placed as shown and I have used the following anchor properties
[TOP,LEFT] [TOP,RIGHT]
[LEFT] [RIGHT]
[BOTTOM,LEFT] [BOTTOM,RIGHT]
I want the buttons to increase their size as well. On the higher resolution, the screen looks empty as all the buttons shift toward the periphery of the screen
If I use the following config, the buttons overlap each other
[TOP,LEFT,RIGHT] [TOP,RIGHT,LEFT]
[LEFT,RIGHT] [RIGHT,LEFT]
[BOTTOM,LEFT,RIGHT] [BOTTOM,LEFT,RIGHT]
What should I do?
I am using Visual C#
Add a TableLayoutPanel to your form, with 3 rows and 2 columns.
Set Dock property to Fill
Put each of your buttons in a cell of the TableLayoutPanel and set their Dock property to Fill
You need to set all of your buttons' "Anchor" properties to Top, Bottom, Left, Right.
This keeps them in place, but also resizes them. Make sure the buttons' "AutoSize"-property is set to false.

How do I make multiple controls in a Windows Form automatically resize with the window?

I'm new to Windows Forms in Visual Studio, and I am wondering how to automaticly resize controls to the window size.
Say, I have 2 controls in a panel, a List Box and a Button. I want the button to dock to the bottom, and I want the List Box to fit the rest of the space. when the window resizes, the button should be at the bottom (as expected with docking), and the list box should stretch down to the button.
Is there a way to do this without any code?
Thanks.
Dock is pretty easy to use, but I recommend using the Anchor properties instead. Resize your form to a reasonable size in the Designer. Then, place your controls to look the way you want. Then, decide which controls should resize with the form and set the Anchor property as follows:
If you want the control to resize with the form in width, set the Right anchor.
If you want to resize height, set the Bottom anchor.
If you want the control to stay right when the form resizes, unset the Left anchor.
If you want the control to stay bottom when the form resizes, unset the Top anchor.
The problem I have with Docks is that they sometimes act funny when controls are not declared in a specific order, and to get the effect you want, sometimes you have to create extraneous panels just to hold controls.
It really gets messy when you want to maintain the aspect ratio of each control. One way, which is not really up to the mark if you want to get into fixing the details, is to use TableLayoutPanel and use Dock and Anchor wisely to achieve what you want.
Use the dock and fill options on the controls. Look under properties for each object, and containers if they are in any.
You can use SplitContainer
Google for examples. Here is one
Try setting your ListBox's Dock property to Fill.
You'll need to watch for one thing though: by default the ListBox will size itself to display whole list items. If you resize the control so that it displays a partial item it will adjust itself so it will display a complete item. This can make the control appear to lose its 'Dock'ing behavior. The solution for this is to set the ListBox's IntegralHeight property to false, which specifies that the control not resize itself to fit complete items.

Categories