Make UserControl resize when Dock = Fill - c#

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.

Related

Menu Strip and Status Strip conceals the form elements

I have a menu strip and status strip whose Dock property is set to Top and Bottom respectively. I need to display a table (i.e. TableLayoutPanel object) programmatically which covers the full form so I have set its Dock property to Fill. The problem is that the status strip and the menu strip covers the top and bottom parts of the TableLayoutPanel object, concealing it. How can I avoid that?
You could add a Panel to your form via the designer. Stretch it out so it covers the area where you want your TableLayoutPanel object, then set the Anchor property of your new Panel to Top, Bottom, Left, Right.
Then just do panel1.Controls.Add(tableLayoutPanel1);
For this problem there is a table layout panel (which you were already using, so you were almost there).
Instead of docking your controls to top or bottom you make a row for the control in the table layout panel and just fully dock it.
Example of table layout panel with 3 rows and controls docked:
As you can see they don't overlap and the tablelayout panel takes care of sizing (you can set % per row, or fixed px etc.)
So what you essentially need is a table layout panel to hold your table layout panel

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 anchor for dynamic size/location 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.

Auto resizing controls on full screen in windows forms

I want to auto resize my windows form controls on fullscreen. I use tableLayoutPanel and anchoring.
But it's not pleasing to the eyes. I used flowLayoutPanel, but it doesn't work. I have around 35 controlrs on one single form, including labels, textboxes, comboboxex, radiobuttons, datagridview and checkbox.
Is there any other method by which I can resize the controls? And if not, can anybody suggest me a way to use the tableLayoutPanel and anchoring more effectively?
It seems to me that what you want to use is the Dock property of all controls as well as using TableLayoutPanel. From the images you provided it looks like you want want the top half of the form to be a TableLayoutPanel, and to set the Dock Property to DockStyles.Fill. Then set the bottom ListView to DockStyles.Bottom.
You can either dock each control in a TableLayoutPanel cell or set the Anchor properties to AnchorStyles.None to make the controls automatically be centered in the cells.

How can I organize controls on a form relative to one another and the form itself?

I have a child form in a MDI Windows Forms application. It has two controls: a ComboBox and a TreeView, with the last one under the first one. Both controls have the same width. How can I set up them and form properties to achieve the following:
When changing the size of the form, a width of both controls must be equal to the width of the form.
The height of the TreeView must be changing to fill all free space of the form.
You can do like this:
In the forms designer, layout the controls like you want them to look
Select the ComboBox, and set the Anchor property to Top, Left and Right
Select the TreeView and set the Anchor property to Top, Left, Right, and Bottom
Basically, you would need to dock your controls. Play with the Dock property of your both controls to find the "docking" which suits your requirements.
Here is an example which demonstrates a Combobox with Dock=Top and TreeView with Dock=Fill:
If you resize the form, the Combobox width and TreeView width/height will be resized accordingly, which suits your specific requirements.
This is done by the Anchor property. Set it properly on all controls (combobox, treeview and usercontrol) and it will stretch however way you like.
The Dock property is similar, but it also affects location and kinda "glues" the control to its place even in the form designer.

Categories