Automatic resizing of the Windows Forms controls - c#

I'm using VS2008's designer for doing this.
For example if I've a windows form of size say 500x500 and I added a DataGridView to it (490x490).
when I run this program. and maximize the form, the DataGridView still remains of the same size. rest of the additional space is blank on the form. I want DataGridView to also take the entire window size.
No software will be like that. I don't know what to change inorder get desired behaviour.

You can use Dock, or for more precise control use the Anchor property. By setting Anchor to Left, Right, Top, Bottom the control will scale with the window. By setting Anchor to Right the control will move with the right border. By setting Anchor to Top and Bottom the control will scale vertically, but have a fixed width. Just experiment

You can ether set the Dock-Property of your DataGridView instance to DockStyle.Fill or use the Anchor-Property and set the anchors to:
dataGridView.Anchor =
AnchorStyles.Bottom |
AnchorStyles.Right |
AnchorStyles.Top |
AnchorStyles.Left;
The first method will make your DataGridView to fill your whole client area. The second method will keep the ratio and only resize the control if the container resizes.

for filled up docking inside the winform use:
dataGridView.Dock = DockStyle.Fill;

Set the Dock property of the DataGridView to Fill.

If you need other things like textbox , picturebox etc as auto scaling then go to their relevant anchor property and set is according to your desire result.

Related

C# Windows Forms - move element when resizing the form

I need some help with a responsive form in WinForms. I have a Checkbox at Position xy. When i resize the form to a smaller size, this Checkbox should move to the left side, closer to the other elements.
You can see it on the pictures I made. I do not know, which Property I have to change there, to make this happen. The checkbox to move is marked by the red box.
When I enlarge the form, this element has to stay at its default position. When I reduce it, the checkbox has to move to the left. When I enlarge it again, the Checkbox has to move back to its default position.
You need to set the Anchor property:
checkBox1.Anchor = AnchorStyles.Top | AnchorStyles.Right;
This states that the right border of the control should always have the same distance to the right border of the containing control (your form).
You can also set this property in the designer window.
Update after your clarification:
This is a complicated situation. One solution I found (though I'm not sure it's the best) could be to use a Panel to contain the CheckBox.
Place the Panel at the left-most position the CheckBox could have
Set the size of the Panel so that the right border is at the right-most border (plus a few pixels) the CheckBox should have
Set the MaximumSize of the Panel to this exact size
Set the Anchor property of the Panel to Top | Left | Right
Place the CheckBox inside the Panel at the right edge
Set the Anchor property of the CheckBox to Top | Right
Now, if you enlarge the Form, the Panel keeps its size because of the MaximumSize value.
If you shrink the Form, the Panel will also shrink because of its AnchorStyle.Right. The CheckBox moves to the left because of its own AnchorStyle.Right.
When you enlarge the Form again, the Panel also grows, but only it reaches its MaximumSize again. The CheckBox moves to the right to keep up with the growing Panel.
Hope this does what you want. I can later add screenshots if necessary.
So i tried to get a clean code solution for this, i hope this will work for everyone:
private void CtrlSequence_SizeChanged(object sender, EventArgs e) // Form got reduced / enlarged
{
checkBox.Location = new Point(Math.Min(Width - checkBox.Width, 345), checkBox.Location.Y); // Width = Forms Width
}
Important! This is just meant for the horizontal movement. The vertical is fixed.
You can use TableLyoutPanel.
With this element you can set Column and Row size in type Percent, and set value "left,right,top,bottom to the properties Anchor for TableLyoutPanel and for all element insaid it.

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

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.

Maintaining the size and position of the Control in the form

I want to maintain size and position of the control relative to its container eg,size and position of the richtextbox in the form should be maintained when form resize.
If I understand you correctly, you need to use the Anchor property.
Have a look at Control.Anchor Property.
If you have more complex designs, just a anchor and docking is not enough. Imagine you have two textboxes and while resizing you like that both boxes grow and shrink (by 50% for each one). In that case you can use the TableLayoutPanel with relative sizes and within these panels you arrange your controls with anchoring and docking.
Another helpful thing: In a more complex design you should take a look into View - Other windows - Document Outline, while you're in Design View and you get a great overview about where which control resides.
Use the 'Anchor' property of the control. Set it to Top, Left, Right, Bottom accordingly to anchor it's edges to the form as required.
You can use the anchor property which will allow you to lock the edges of your control relative to the windows edge.
This is what the designer adds in when setting Top, Bottom, Left and Right
this.textBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
Alternatively a docking panel can be useful in keeping controls docked to one side of a window.

Categories