PictureBox anchor not working properly - c#

On my form I have a Panel control that contains a PictureBox control and a Label control.
The panel is not visible in the image above but it's basically the area around these two controls.
I have set the Anchor property of all these three controls to Top, Bottom, Left, Right so that they follow their parent container's re-sizing behavior.
The L abel control (postbagfolderempty) works properly but the PictureBox (EMPTY!) does not seem to be moving from its original position.
Is there an additional property I need to set?
Update: I changed my PictureBox's AutoSize property to None. It has started to move, but as I try to enlarge my form, it starts sinking into a white area (image below).

Make sure your PictureBox doesn't have SizeMode set to AutoSize.
Anchoring changes the size, if it's autosized it won't change anything
Also, make 100% sure your PictureBox is actually a child of the panel. It's easy to check: select it on the designer and press Esc, it should select the panel.
Update
As per the comments, seems the problem is that you are anchoring to all the sides (thus producing the scaling of the control).
If you want a panel that scales with the form, and controls within the panel that are centered but not scaled along, then anchor that panel to all sides, place the controls inside the panel centered in the designer, and set their anchors to None, that way they won't scale, and since they are not anchored, they will move along when the panel scales (but they won't scale with it, which -seems- it's what you are aiming at)

Related

How to make controls stretch and shrink when the window is resized?

How do I make it when the window is resized none of the controls are on one corner or just shrug away but become bigger or smaller to correspond with the window In Windows forms?
You need to Anchor your control(s) on the window.
Basically u (always) anchor your control on Top & Left (that's default anyway).
If you want to stretch your Control horizontal add Right.
For vertical stretching add Bottom.
[Properties] (rightclick the control you want to anchor and select properties)
Use Anchor property of each control to achieve the effect.
For example, if you set it to "Right, Bottom" on a control, it will keep its right and bottom side anchored (set to fixed distance) against the right and bottom edge of the form.
This is sufficient for basic sizing. For advanced sizing, you have to size your controls manually on window resize event.
You might also want to set MinimumSize property of your form to prevent its window to shrink under that size. This way you can prevent unwanted layout distortions like controls clipped or completely hidden behind right or bottom edge of the window.

What is the difference between Dock and Anchor

I have a windows form which have a lot of controls in
that(Listbox,Groupbox,Combobox,TextBox,ListBox etc). I need to resize
and arrange the controls automatically whenever the form's size gets
changed. I need to know the difference between Dock and Anchor to
implement this. What is the actual difference between Docking and
Anchoring?
The Anchor and Dock properties of a form are two separate properties.
Anchor refers to the position a control has relative to the edges of the form. A textbox, for example, that is anchored to the left edge of a form will stay in the same position as the form is resized.
Docking refers to how much space you want the control to take up on the form. If you dock a control to the left of the form, it will stretch itself to the height of the form, but its width will stay the same.
This EXAMPLE can help you understand a bit more.

The wider I make my user control, the more gets cut off on the right

I have a user control containing 2 child controls:
A label on the left with Anchor set to Left|Right|Top|Bottom
A picture box on the right with Anchor set to Right|Top|Bottom.
I expect the picture box to stay attached to the right side of the control, which it does in the designer. However, when I actually run my program, the picture box seems to float off the right side of the control if I make the control wider than about 100 pixels, and at 150 pixels, the picture box (which is 20x20) is completely invisible!
I don't have any code adjusting the anchor style of the picture box at runtime, so how can this be happening and how can I fix it?
I solved this problem by setting the AutoScaleMode property in the parent control to None.
Actually, using Dock instead of Anchor seems to have solved my problem! I set the picture box to dock right, and the label to dock fill, and now it seems to look right!
May I suggest you to use the Anchor property, instead of the Dock property, with the same values.
I think just using Anchors instead of setting Docks may help:
If you intend PictureBox just to be attached to the right side of control - use Anchor property set to Right | Top (but it can overlay label on the left in case, when control width will be to small)
If you intend to stretch PictureBox when control is resized vertically then set Anchor = Right | Top | Bottom.
If you want PictureBox to stretch vertically and horizontally when container control resized set all Anchors (in that case label won't be overlayed by PictureBox)
Labels Anchor is better to set to just Left | Top, because anchoring it to Left | Right, may lately, in case with different TextAlign values, be causing shifts of text across the control when resizing

Center buttons in a container

How does one specify that a button centers itself in a container without having to specify a Location? Is it even possible?
The goal is to be able to center multiple buttons in a panel without having to perform calculations on their placement.
I know it is possible to center some controls on a form, not sure about a panel though. Anyway:
Disable the Left and Right anchors of your control if you want your controls to stay centered horizontally, and the Top and Bottom anchors if you want your controls to stay centered vertically,
In the designer window, select your control,
In the VS 'Format' menu, hit 'Center in form', then 'Horizontally' and/or 'Vertically'.
If you want to center several controls side to side, select them all and execute the above steps.
Controls will then stay centered in the form when the user resizes the window.
I'm not 100% sure of what you are asking, but try using a TableLayoutPanel, and drop one button in each cell of the table. If you anchor the TableLayoutPanel to the Top, Left, Bottom& Right, the Table will grow and shrink with the form, but each button will "float" relative to the top-left corner of it's containing cell.
Disabling all anchoring will keep the TableLayoutPanel at it's relative location within your form, but your buttons will remain spaced out evenly amongst one another.
Between standard control anchoring and/or the the TableLayoutPanel you should be able to find the correct type of anchoring that you desire.

Scrollbar does not appear on Panel when lines drawn on it (C#)

I want to draw some lines and rectangles on a panel. Sometimes it does not fit in panel
and I want the scroll bar to appear. I set AutoScroll property to true, but it doesn't work ;(
Set the panel's AutoScrollMinSize property to a something larger than the panel's real dimensions (for example, if your panel is 300 x 200, set the AutoScrollMinSize property to 900 x 600). This will cause both scrollbars to appear, and you should be able to draw on the larger surface.
You will need to tell the panel control that you are drawing outside of the visible bounds by setting the AutoScrollMinSize property. But another, perhaps simpler, solution would be to have your panel contain another panel control in which you do the drawing. Then you can simply resize that inner panel to fit your drawing and the outer panel will automatically provide the scrolling as necessary.

Categories

Resources