C# Winforms, no resize arrow using GrowAndShrink - c#

I have a winform with a picture box, a button, and a menustrip. The picture box is anchored to all sides so when I resize the form, the picture box resizes as well. I set the form to have a minimum size of 700x600.
But this only works when the form is set to AutoSizeMode = GrowOnly. If I change to AutoSizeMode = GrowAndShrink, the diagonal <=> resize arrow doesn't even show up.
If I set the SizeGripStyle = Show on the form, I can get the arrow to show up and "resize" but as I drag it to resize, it just flickers really fast and goes back to default size.
How can I make it GrowAndShrink instead of just GrowOnly?

Make sure the form properties have the following:
AutoSize: false (sounds like it should be true, but set this to false).
AutoSizeMode: GrowOnly (like above, sounds like it should be GrowAndShrink)
MinimumSize: Not important. set to 1, 1 for now. Is just to stop resizing getting too small.
MaximumSize: Not important. set to 1, 1. As above (MinimumSize).
SizeGripStyle: Not important. Set to Show.
Lastly, ensure that you use anchoring or docking for adjusting control widths when the form resizes. Setting controls with a width may prevent you resizing the form.

Try setting the picture box's AutoSizeMode = GrowAndShrink.
I usually leave the form's AutoSizedMode = GrowOnly, because if you forget to set some of the minimum sizes on your controls, it will always try to be the minimum size.

Related

How can I link a component's Size to the Window size?

I am making a simple WinForms program.
I would like to link a component's size to the size of the Window.
Let's say the user enlarges or shrinks the Window by dragging it's borders: I would like that a component gets bigger when the Window does and vice versa.
Let's pretend that we have two Buttons, in the center of the Window, side by side: I want to make them the same size filling the entire width of the Window.
How can I do that?
Sample procedure, using a TableLayoutPanel and 2 Buttons:
Add a TableLayoutPanel to the Form
Edit the Columns and Rows collections so that you have 2 Columns, both sized at 50% and one Row, set to Autosize.
Set the Location.X of the TableLayoutPanel to 0 and adjust its width to the width of the Form.
Set the TLP Anchor property to Left and Right
Adjust-drag the Row height to be ~twice the size of the Button it will host
Add one Button to the Form
Adjust the appearance of the Button as required.
CTRL-Drag the Button to create an exact duplicate
Add the two Buttons to the two cells of the TableLayoutPanel
SHIFT-Select both Buttons and set the Dock property to DockStyle.Fill
You can now adjust the Margin property of the Buttons (still both selected, so the same settings will apply to both) to modify the space between the controls
Re-adjust the TableLayoutPanel's only Row height as needed.
Extra: if you have only these controls on the Form, you may want to fix the MinimumSize of the Form, to avoid that, when the Form is resized, your controls are shrinked beyond recognition, ruining the overall layout: resize the Form in the Designer to a point where the hosted controls layout is compromised, find a suitable minimum size and use this measure as the Form's MinimumSize property. The MinimumSize can be set using only the Width or the Height measure (e.g., (100, 0)). This limits the Form's Width but not the Height. Or the opposite, of course.
If, when dragging the Buttons inside the TableLayoutPanel, the Buttons
are not automatically inserted at the Top-Left position of the cell
and instead they appear positioned in a random place, then the
TableLayoutPanel has gone rogue and needs to be put down. Delete it
and drop another on the Form. Rinse and repeat.
This may happen if you tamper with the layout a bit. Better start over than trying to correct the problem.
TableLayoutPanel Control Overview

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

change the height of statusStrip

I have a simple windows form with a statusStrip in VS2010, and no matter what I tried, the height of statusStrip does not change, what is proper way of changing the height??
thanks
I just changed the StatusStrip size without problems...
Create a new Form.
Create a StatusStrip.
Set its property "Dock" to "None".
Set its property "Autosize" to "False".
Set its property "Size" with Height = 'the height you need' (I have put it to 100).
If you want, now you can Dock to "Bottom" again.
Add a ProgressBar: it will be 100 Height.
Let me know if this works for you
None of the mentioned approaches worked. Eventually this is the working solution for us:
First, use a TableLayoutPanel with a row at 100% and bottom row with absolute height of 24 (or any desired height you want).
Second, drag and drop the ToolStrip control into the bottom row, and setting the Dock property to Fill. This forces the ToolStrip control to fill the bottom cell of the TableLayoutPanel. Now you have a toolstrip with height 24.

Categories