How to anchor for dynamic size/location C# - c#

I don't know how to ask so this is the example:
So my question is how to avoid two buttons overlapping each other ? How do i need to anchor them or to make the location dinamic ?
In the example both buttons are anchored left,top,right. I need them to auto-size when i maximize.

Put them in a panel docked left and right. Dock them both to fill but dock the right button first.

Well, if you want them to each have equal sizes, try putting them into a table, so that each column of the table is 50% width. Then set the table to be anchored left, right, and top.

Using the Anchor property of the controls the way you did it should work and is the simplest way to achieve the desired effect. Just make sure you first size and position both buttons the way you want them in the designer (so that they just look right in the designer) and then set the anchor of both buttons to left and right and top. They will then resize and reposition themselfes proportionally when you resize the form.
Alternatively you can put the two buttons on a TableLayoutPanel with two columns or on a regular Panel and use the Dock property, dock one button to the left and one button to the right.

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!

How do I set dock property of a datagridview properly?

In a datagridview when I set:
Anchor: Top,Left
AutoSizeColumn: Fill
Dock: Fill
Which will expand or shrink the datagridview based on size of the form, and covers the whole form. But what of I have controls on the right side on the form? I don't want to overlap my linkbuttons with my gridview. I know there's a "Margin" and "Default Cell Style" property with padding values (specifically using "right padding" to try and reduce width from the right of the right edge of the form). So I tried messing with that, didn't do anything I want. Or was I working with the right properties, just not setting them right?
Put those controls you want on the right in a Panel.
Set the DockStyle of that panel as DockStyle.Right.
If you can't see the right edge of the Datagrid means, you have to bring the Datagrid to front by selecting it, right click and choose bringToFront.
Or Send the Panel to back.
1.Use a panel to split the form into two sections.
The right section is placed with DataGridView, and the left section is placed with a panel which is used to contain your other controls, like linkbuttons.
2.Set the Dock property of this panel to Right.
3.Set the Dock property of this DataGridView to Fill.

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.

C# positioning buttons

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

Make UserControl resize when Dock = Fill

I have a problem with UserControl that I'm crafting. It consists of TableLayoutPanel with another TableLayoutPanel in it with ListView inside. I want to make it resizable so that it will fit in left panel of my app and behave somewhat like Toolbox in Visual Studio. The problem is that my control doesn't scale when I resize panel.
UserControl is embedded in panel with Dock = Fill and Anchor = Tob, Left, Bottom, Right. Also all controls in it are made that way. How can I fix this?
EDIT: It's WinForms, not XAML.
Use a split panel and put your UserControl in the left panel and Dock.Fill it.
You're probably looking for the AutoSize properties on the TableLayoutPanel and the AutoSize ColumnType of that panel.
You can achieve something like a Dock = Fill by simply auto-sizing the table layout panel (GrowAndShrink) so that it will always fit your inner control.
Please post your designer code to see how you embedded the controls in which other control.
I suspect your resize problem come from your resizing strategy of control inside the TableLayoutPanel.
The table layout panel is tricky. Regarding the resize strategy you want to follow inside a cell of the table panel, the control in the cell have either to be Dock.Fill or Anchor = Top, Left, Bottom, Right.
Basically:
Il you want the grid cell to adapt to the size of the control, then have the control in the cell Anchor = Top, Left, Bottom, Right and set the row/column to autosize.
If you want the control in the cell to adapt to the cell size, use Dock.Fill on it and use a percentage or a absolute value to size your cell.
The behavior of the TableLayoutPanel is best described in the MSDN documentation.

Categories