I just wondering about my windows Application. When i re-size windows form that time my Form controls should re-size. Can any body tell me how to do this I have Used Anchor property for that but no luck.
This is a win form.
there are two ways to make a control automatically resize based on size changes of the :
Set the Dock property of the control to DockStyle.Fill.
Set the Anchor property to "Top, Bottom, Left, Right"
But if you decide to use WPF GridLayout,it has a lot Capability for this target.
Edited
See this short tutorial Resizing controls with form: Anchor property Tutorial
You have to use the anchor property in properly.
For example, for DataGridview to resize in its all direction, you have to set its anchor property as Top, Bottom, Left, Right
You have to set the anchor property suitably for each controls in the form as per your requirement.
Hope this helps.
If you set the Dock property of your control to Fill. You could also take a look at the tableLayoutPanel if you have more controls which you want to keep aligned while resizing your form.
Related
I'm working on a .NET 3.5 desktop application written in C#. It's complex UI is populated dynamically. One part of it is the following group box which is contained in a FlowLayoutPanel and the FlowLayoutPanel is contained in a UserControl. The screenshot is taken from the design view:
When I launch the application, all controls get stretched:
Even I'm fixing the widths of each UserControl's size when Load event of the UserControl is called. The AutoSize property of all of the controls inside the group box is false.
Why is this happening and how to prevent this? I want the UI look exactly like the design view.
EDIT
The best answer to this question didn't solve my problem. Firstly, setting the border style to FixedX creates an undesirable border. Secondly, the inner controls still expanded and they are clipped by the border.
Make AutoScaleMode "Inherit" for all children user control.
You can play in the Anchor property
If you want to keep the Label in its place as the design => use combination : Top, Left
If you want to stretch the control horizontally => use combination : Top, Left , Right
If you want to keep the control to stretch the control in all directions => use combination : Top, Left, Right, Bottom
If you want to keep the control in the Bottom right position ( usually used for buttons) => use combination: Bottom, Right
You might read more about Anchor here and here
Hope this will help you
Set the WrapContents property of your FlowLayoutPanel to true
Please notify my if this worked or not
Check EnableVisualStyles property value set at application level. Set it to true and check.
If your button gets wider when you enlarge its container object, the button must be surely anchored to the left and right borders of the container. Just anchor it to the top or bottom, but not to the left or right borders.
Conversely, if your button gets higher, don't anchor it to the top or bottom.
Why does the WinForms SplitContainer hide all the Buttons in a Panel (in the left Splitter) when I resize on of its panels? Do I have to write specific code for invalidation of the Panel?
Yes, there's a solution. You can use Anchor property of SplitContainer in winforms. Using which you can set the value of the anchor as you required.
EDIT:
Anchor property will make your control fixed to the winforms design. Even if you resize the control, all the controls that the major control is holding will also resize according to it.
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.
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.
I am working on a largish C# project with a lot of Windows Forms forms that, even though you can resize the form, the elements in the form don't scale.
How can I make the form elements (such as the datagridview, text area's, etc.) scale when the user changes the size of the form?
Nearly all the forms subclass from one specific form, so if there's something I can do in the base class, that'd be great.
You should set the Anchor and Dock properties on the controls in the forms.
The Anchor property controls which edges of a control are "bound" or "tied" to the corresponding edges of its form.
For example, if you set Anchor to Bottom, the distance between the control's bottom edge and the bottom of its parent will not change, so the control will move down as you resize the form.
If you set Anchor to Top | Bottom, the control will resize vertically as you resize the form.
To make a control resize with the form, set the Anchor to all four sides, or set Dock to Fill.
Use the Anchor and Dock properties.
Anchor allows you to pin specific sides of the control to the sides of the parent control.
Dock will bind the whole control to a side of the parent control or it can be set to fill the contents of the parent control.
You usually just need to set the Anchor to the bottom and right of the parent control but gets more difficult when you have controls side by side, then you need to manually resize the controls on the forms OnResize event to get them to scale naturally together.