C# Windows Forms - move element when resizing the form - c#

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.

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

TextBox size change when re-sizing the form

Many programs re-size their text boxes, labels, picture boxes, etc... when you change the the whole form's size. But when I drag a text box in my form and make the form smaller, it will overlap the text box and it'll be unusefull because I can't see everything which is written in there.
It's hard to explain so here are some photo's:
Check the property Anchor for your textbox.
It appears that you need to set to Top, Left, Right
If you set in this way, the textbox remains anchored to its container left, top and right borders.
Thus, when the form (the container) is resized, the textbox automatically grows or shrinks to maintain the original distance set in the designer.
Click Dock dropdown menu and select desired location
In WPF, the textbox will re-size automatically

How to change position of inherited items in an Inherited user control

I have used a user control as a base class (let's call it BaseUC) with 3 labels (in 3 lines) on it (they are set as protected).
And there is another user control that inherits from it (InheritedUC). I have added two more labels in InheritedUC, which are positioned between the base's labels (so there are 5 lines).
Everything is fine is Visiual Studio's design UI view. But when I run the application, labels on BaseUC overlap with the ones in InheritedUC and I can't see the ones on the inherited control.
Any ideas to fix this? Thank you very much
From MSDN: Control.Anchor Property
Use the Anchor property to define how a control is automatically
resized as its parent control is resized. Anchoring a control to its
parent control ensures that the anchored edges remain in the same
position relative to the edges of the parent control when the parent
control is resized.
You can anchor a control to one or more edges of its container. For
example, if you have a Form with a Button whose Anchor property value
is set to Top and Bottom, the Button is stretched to maintain the
anchored distance to the top and bottom edges of the Form as the
Height of the Form is increased.
Set the Anchor property on all labels:
For example:
label1.Anchor = AnchorStyles.Top | AnchorStyles.Left;
If you put your controls in a FlowLayoutPanel and set the following options:
AutoScroll = True
FlowDirection = TopDown
WrapContents = False
Then you should get panel that will grow and shrink as your controls are added or removed.
Source

C# Fix the size of picture box even when the size of the windows is changed

May I know how do we fixed the size of the picture box? Because for now, everytime I drag to change the winform size, the size of the picture box on it will change too.
You have set your anchor property of your PictureBox to:
Top, Bottom, Left, Right
You may set it to
Top, Left
Look in the properties for the picture box, Currently it will be:
Check Anchor property of your PictureBox and make sure the Dock property is set to None;
Use the Anchor property to define how a control is automatically resized as its parent control is resized:
Control.Anchor: Gets or sets the edges of the container to which a control is bound and determines how a control is resized with its parent.

Automatic resizing of the Windows Forms controls

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.

Categories