Margin is not being applied - c#

I have a user control that I need to manually resize during runtime, although I would like the margin to be applied. I found, from link 1 below, that for the margin to work the AutoSize property needs to be set to true. Although, since I need to manually resize the control during runtime, the AutoSize property needs to be false to my understanding.
The user control (Accordion) contains a horizontal split panel where the first panel contains a button and the second panel contains a Panel control. In the image below you can see multiple "Accordions" which, in the picture, is one of the buttons. The Accordions are docked at the top of the container. The intent is when I click on the button it resizes and shows the "contents panel" and the contents panel's control. My problem is that setting the User Control's margin property does not cause them to automatically distance themselves from the other controls nor the container they are in.
Link 1: WinForm Bottom Margin Property Doesn't Do Anything
Hopefully some useful chunks of what I'm working with.
namespace CustomControls
{
partial class Accordion
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.split = new System.Windows.Forms.SplitContainer();
this.ToggleCollapse = new System.Windows.Forms.PictureBox();
this.MainButton = new System.Windows.Forms.Button();
this.contentsPanel = new System.Windows.Forms.Panel();
((System.ComponentModel.ISupportInitialize)(this.split)).BeginInit();
this.split.Panel1.SuspendLayout();
this.split.Panel2.SuspendLayout();
this.split.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.ToggleCollapse)).BeginInit();
this.SuspendLayout();
//
// split
//
this.split.Dock = System.Windows.Forms.DockStyle.Fill;
this.split.FixedPanel = System.Windows.Forms.FixedPanel.Panel1;
this.split.IsSplitterFixed = true;
this.split.Location = new System.Drawing.Point(0, 0);
this.split.Margin = new System.Windows.Forms.Padding(3, 3, 3, 10);
this.split.Name = "split";
this.split.Orientation = System.Windows.Forms.Orientation.Horizontal;
//
// split.Panel1
//
this.split.Panel1.Controls.Add(this.ToggleCollapse);
this.split.Panel1.Controls.Add(this.MainButton);
this.split.Panel1MinSize = 0;
//
// split.Panel2
//
this.split.Panel2.Controls.Add(this.contentsPanel);
this.split.Size = new System.Drawing.Size(663, 488);
this.split.SplitterDistance = 40;
this.split.TabIndex = 0;
//
// ToggleCollapse
//
this.ToggleCollapse.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.ToggleCollapse.BackColor = System.Drawing.SystemColors.ActiveCaption;
this.ToggleCollapse.Enabled = false;
this.ToggleCollapse.Image = global::CustomControls.Properties.Resources.TrianglePointer;
this.ToggleCollapse.Location = new System.Drawing.Point(621, 13);
this.ToggleCollapse.Name = "ToggleCollapse";
this.ToggleCollapse.Size = new System.Drawing.Size(39, 24);
this.ToggleCollapse.SizeMode = System.Windows.Forms.PictureBoxSizeMode.CenterImage;
this.ToggleCollapse.TabIndex = 0;
this.ToggleCollapse.TabStop = false;
//
// MainButton
//
this.MainButton.AutoEllipsis = true;
this.MainButton.BackColor = System.Drawing.SystemColors.ActiveCaption;
this.MainButton.Dock = System.Windows.Forms.DockStyle.Fill;
this.MainButton.FlatAppearance.BorderColor = System.Drawing.Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(255)))), ((int)(((byte)(192)))));
this.MainButton.FlatAppearance.BorderSize = 3;
this.MainButton.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.MainButton.Font = new System.Drawing.Font("Verdana", 18F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.MainButton.ImageAlign = System.Drawing.ContentAlignment.MiddleRight;
this.MainButton.Location = new System.Drawing.Point(0, 0);
this.MainButton.Name = "MainButton";
this.MainButton.Size = new System.Drawing.Size(663, 40);
this.MainButton.TabIndex = 0;
this.MainButton.Text = "Accordion Activator";
this.MainButton.TextAlign = System.Drawing.ContentAlignment.BottomLeft;
this.MainButton.UseVisualStyleBackColor = false;
this.MainButton.MouseDown += new System.Windows.Forms.MouseEventHandler(this.MainButton_MouseDown);
this.MainButton.MouseEnter += new System.EventHandler(this.MainButton_MouseEnter);
this.MainButton.MouseLeave += new System.EventHandler(this.MainButton_MouseLeave);
this.MainButton.MouseUp += new System.Windows.Forms.MouseEventHandler(this.MainButton_MouseUp);
//
// contentsPanel
//
this.contentsPanel.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.contentsPanel.Dock = System.Windows.Forms.DockStyle.Fill;
this.contentsPanel.Location = new System.Drawing.Point(0, 0);
this.contentsPanel.Name = "contentsPanel";
this.contentsPanel.Size = new System.Drawing.Size(663, 444);
this.contentsPanel.TabIndex = 0;
this.contentsPanel.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Panel_MouseDown);
this.contentsPanel.MouseLeave += new System.EventHandler(this.Panel_MouseLeave);
this.contentsPanel.MouseMove += new System.Windows.Forms.MouseEventHandler(this.Panel_MouseMove);
this.contentsPanel.MouseUp += new System.Windows.Forms.MouseEventHandler(this.Panel_MouseUp);
//
// Accordion
//
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Inherit;
this.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.Controls.Add(this.split);
this.DoubleBuffered = true;
this.Margin = new System.Windows.Forms.Padding(3, 3, 3, 10);
this.Name = "Accordion";
this.Size = new System.Drawing.Size(663, 488);
this.split.Panel1.ResumeLayout(false);
this.split.Panel2.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.split)).EndInit();
this.split.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.ToggleCollapse)).EndInit();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.SplitContainer split;
private System.Windows.Forms.Button MainButton;
private System.Windows.Forms.PictureBox ToggleCollapse;
protected System.Windows.Forms.Panel contentsPanel;
}
}
This is when and how I manually resize
public bool Collapsed
{
get
{
return split.Panel2Collapsed;
}
set
{
Freeze(true);
if (Dock == DockStyle.Fill && this.DesignMode)
{
if (firstResize)
{
if (value)
this.Parent.Height = MainButtonHeight;
else
this.Parent.Height = ExpandedHeight;
}
else
{
if (value)
CollapseAnimation(1);
else
ExpandAnimation(5);
}
}
else
{
if (firstResize)
{
if (value)
this.Height = MainButtonHeight;
else
this.Height = ExpandedHeight;
if (loadBegan)
firstResize = !firstResize;
}
else
{
if (value)
CollapseAnimation(1);
else
ExpandAnimation(5);
}
}
split.Panel2Collapsed = value;
ToggleCollapse.Image = toggleImageNeutral;
Freeze(false);
}
}

Not sure I completely understood your problem but maybe this can be of help. If you want a fixed margin to stay around your control after a resize (due to the SplitContainer panel extending/collapsing), you can try this:
on your blank user control drag a Panel control and set its Margin to (0,0,0,0) and Padding to (x,x,x,x), where x is the width of the margin you want.
Be careful when doing calculations on the height of the children controls to resize the control, always take the paddings and margins into account if you have them set on some children.
UPDATE:
As stated here: https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.control.margin?view=netframework-4.8
"Setting the Margin property on a docked control has no effect on the distance of the control from the edges of its container."
So, I think you have to choose a specific container like TableLayoutPanel or a FlowLayoutPanel. Try with a FlowLayoutPanel and set it like this:
AutoSize = true;
BorderStyle = FixedSingle (just to see if the margin is kept correctly)
FlowDirection = TopDown
Then drag inside of it some of your controls with the AutoSize property set to false and with the Margin set as you prefer. You should get something like this:
Note: the height of your controls may still be changed (maybe setting a minimum control height can be of help), but once you set their width, the FlowLayoutPanel control can't cut the control because of the margin.

Related

Resize 4 picture boxes when form is resized

I am implementing a winform application in c# so that it contains four picture boxes and some control buttons in one corner.
My main problem is that I cannot achieve the behaviour shown in the picture. The idea is to automatically resize the picture boxes when the form is resized. The area of the control panel should stay the same and only the area containing the boxes should be resized accordingly.
I have been playing with anchor and dock properties but I dont get the desire output.
Anyone could give me a hint?
Thanks and kind regards,
Bilbinight
So, let's say you have the following picture boxes:
pictureBox1 | pictureBox2 | panel
------------|------------
pictureBox3 | pictureBox4
Then the following should do the trick:
set the forms Resize event to this eventhandler:
private void Form1_Resize(object sender, EventArgs e)
{
int spaceBetweenPictures = 19;
int widthToFill = (this.Width - 40 - panel.Width) - spaceBetweenPictures;
int heightToFill = this.Height - 80;
pictureBox1.Width = widthToFill / 2;
pictureBox1.Height = heightToFill / 2;
// Setting the sizes of all the three pictureboxes to the sizes of the first one.
pictureBox2.Width = pictureBox1.Width;
pictureBox2.Height = pictureBox1.Height;
pictureBox3.Width = pictureBox1.Width;
pictureBox3.Height = pictureBox1.Height;
pictureBox4.Width = pictureBox1.Width;
pictureBox4.Height = pictureBox1.Height;
// Setting the positions:
pictureBox2.Location = new Point(pictureBox1.Width + spaceBetweenPictures, pictureBox1.Location.Y);
pictureBox3.Location = new Point(pictureBox1.Location.X, pictureBox1.Height + spaceBetweenPictures);
pictureBox4.Location = new Point(pictureBox2.Location.X, pictureBox3.Location.Y);
}
Of course you should modify the magic numbers in this code (19, 40, 80) accordingly, to suit your program (that depends a lot on whether you use border on your form or not).
UPDATE:
If you want your pictureboxes square shaped then just ignore the heightToFill variable and use widthToFill instead when setting the Height of pictureBox1. Or set:
pictureBox1.Height = pictureBox.Width;
And I also forgot to mention that the panel of course should be aligned Top, Right. So set the panel's Anchor property to:
AnchorStyles.Top | AnchorStyles.Right;
Are four image boxes supposed to support resizing at runtime (allowing end user to resize)? if not then following will give you desired result.
1 TableLayoutPanel with 2 rows and two columns. Set the initial size to fill the desired area, set the anchor to all four directions.
Add 4 picture boxes to 4 cells and set Dock to Fill for all Picture Boxes.
Add a panel to the right side of form, and make it fit to cover your controls area. Set the anchor Top, Bottom, Right.
Here is sample code:
//frmResizing.cs
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class frmResizing : Form
{
public frmResizing()
{
InitializeComponent();
}
}
}
//frmResizing.Designer.cs code
namespace WindowsFormsApplication1
{
partial class frmResizing
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel();
this.panel1 = new System.Windows.Forms.Panel();
this.pictureBox4 = new System.Windows.Forms.PictureBox();
this.pictureBox3 = new System.Windows.Forms.PictureBox();
this.pictureBox2 = new System.Windows.Forms.PictureBox();
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.tableLayoutPanel1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox4)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.pictureBox3)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.pictureBox2)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
this.SuspendLayout();
//
// tableLayoutPanel1
//
this.tableLayoutPanel1.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)));
this.tableLayoutPanel1.ColumnCount = 2;
this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
this.tableLayoutPanel1.Controls.Add(this.pictureBox4, 1, 1);
this.tableLayoutPanel1.Controls.Add(this.pictureBox3, 0, 1);
this.tableLayoutPanel1.Controls.Add(this.pictureBox2, 1, 0);
this.tableLayoutPanel1.Controls.Add(this.pictureBox1, 0, 0);
this.tableLayoutPanel1.Location = new System.Drawing.Point(2, 1);
this.tableLayoutPanel1.Name = "tableLayoutPanel1";
this.tableLayoutPanel1.RowCount = 2;
this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
this.tableLayoutPanel1.Size = new System.Drawing.Size(544, 561);
this.tableLayoutPanel1.TabIndex = 0;
//
// panel1
//
this.panel1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Right)));
this.panel1.BackColor = System.Drawing.Color.Red;
this.panel1.Location = new System.Drawing.Point(553, 1);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(107, 561);
this.panel1.TabIndex = 1;
//
// pictureBox4
//
this.pictureBox4.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBox4.Image = global::WindowsFormsApplication1.Properties.Resources.download;
this.pictureBox4.Location = new System.Drawing.Point(275, 283);
this.pictureBox4.Name = "pictureBox4";
this.pictureBox4.Size = new System.Drawing.Size(266, 275);
this.pictureBox4.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
this.pictureBox4.TabIndex = 3;
this.pictureBox4.TabStop = false;
//
// pictureBox3
//
this.pictureBox3.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBox3.Image = global::WindowsFormsApplication1.Properties.Resources.pizza_page;
this.pictureBox3.Location = new System.Drawing.Point(3, 283);
this.pictureBox3.Name = "pictureBox3";
this.pictureBox3.Size = new System.Drawing.Size(266, 275);
this.pictureBox3.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
this.pictureBox3.TabIndex = 2;
this.pictureBox3.TabStop = false;
//
// pictureBox2
//
this.pictureBox2.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBox2.Image = global::WindowsFormsApplication1.Properties.Resources.images;
this.pictureBox2.Location = new System.Drawing.Point(275, 3);
this.pictureBox2.Name = "pictureBox2";
this.pictureBox2.Size = new System.Drawing.Size(266, 274);
this.pictureBox2.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
this.pictureBox2.TabIndex = 1;
this.pictureBox2.TabStop = false;
//
// pictureBox1
//
this.pictureBox1.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBox1.Image = global::WindowsFormsApplication1.Properties.Resources.download__1_;
this.pictureBox1.Location = new System.Drawing.Point(3, 3);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(266, 274);
this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
this.pictureBox1.TabIndex = 0;
this.pictureBox1.TabStop = false;
//
// Form2
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(660, 562);
this.Controls.Add(this.panel1);
this.Controls.Add(this.tableLayoutPanel1);
this.Name = "Form2";
this.Text = "Form2";
this.tableLayoutPanel1.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.pictureBox4)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.pictureBox3)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.pictureBox2)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.TableLayoutPanel tableLayoutPanel1;
private System.Windows.Forms.PictureBox pictureBox4;
private System.Windows.Forms.PictureBox pictureBox3;
private System.Windows.Forms.PictureBox pictureBox2;
private System.Windows.Forms.PictureBox pictureBox1;
private System.Windows.Forms.Panel panel1;
}
}

Relocating panels gets mixed up WinForms

I am creating an application using C# and WinForms. I have three panels. Two of them are fairly large and are currently positioned side by side. I am trying to implement a feature for users who have smaller screens or wish to have the form as a smaller size. I am relocating the right panel underneath the middle one and re-centering them in the form.
The issue that I have though is when the user scrolls down and re-sizes the form again the two middle panels (the big ones) move lower in the form leaving a chunk of blank space at the top.
My code is fairly simple
namespace ResizeCheck
{
public partial class Form1 : Form
{
Point originalLeft, originalRight;
bool flag = false;
public Form1()
{
InitializeComponent();
originalLeft = leftInnerPanel.Location;
originalRight = rightInnerPanel.Location;
}
private void vertical()//move the right panel under the left one
{
leftInnerPanel.Location = new Point(this.Width / 2 - leftInnerPanel.Width / 2, 5);
if (leftInnerPanel.Location.X <= buttonPanel.Location.X + buttonPanel.Width)
{
leftInnerPanel.Location = new Point(buttonPanel.Location.X + buttonPanel.Width, leftInnerPanel.Location.Y);
}
rightInnerPanel.Location = new Point(leftInnerPanel.Location.X, leftInnerPanel.Height + 10);
MessageBox.Show("inside vertical " + leftInnerPanel.Location.Y);
}
private void horizontal()//relocate to their original horizontal position
{
leftInnerPanel.Location = new Point(buttonPanel.Location.X + buttonPanel.Width + 10, 5);
if (leftInnerPanel.Location.X <= buttonPanel.Location.X + buttonPanel.Width)
{
leftInnerPanel.Location = new Point(buttonPanel.Location.X + buttonPanel.Width, leftInnerPanel.Location.Y);
}
rightInnerPanel.Location = new Point(leftInnerPanel.Location.X + leftInnerPanel.Width + 20, 5);
MessageBox.Show("inside horizontal " + leftInnerPanel.Location.Y);
}
private void Form1_SizeChanged(object sender, EventArgs e)//handler for when the form is resized by the user
{
if ((leftInnerPanel.Width + rightInnerPanel.Width + buttonPanel.Width) >= this.Width)
{
vertical();
//flag = true;
}
else if ((leftInnerPanel.Width + rightInnerPanel.Width + buttonPanel.Width) + 50 < this.Width)
{
horizontal();
//flag = false;
}
}
}
}
My designer code is also fairly simple.
namespace ResizeCheck
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.rightInnerPanel = new System.Windows.Forms.Panel();
this.leftInnerPanel = new System.Windows.Forms.Panel();
this.buttonPanel = new System.Windows.Forms.Panel();
this.SuspendLayout();
//
// rightInnerPanel
//
this.rightInnerPanel.BackColor = System.Drawing.Color.Yellow;
this.rightInnerPanel.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.rightInnerPanel.Location = new System.Drawing.Point(887, 13);
this.rightInnerPanel.Name = "rightInnerPanel";
this.rightInnerPanel.Size = new System.Drawing.Size(662, 936);
this.rightInnerPanel.TabIndex = 1;
//
// leftInnerPanel
//
this.leftInnerPanel.BackColor = System.Drawing.Color.Yellow;
this.leftInnerPanel.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.leftInnerPanel.Location = new System.Drawing.Point(219, 13);
this.leftInnerPanel.Name = "leftInnerPanel";
this.leftInnerPanel.Size = new System.Drawing.Size(662, 936);
this.leftInnerPanel.TabIndex = 0;
//
// buttonPanel
//
this.buttonPanel.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
this.buttonPanel.Location = new System.Drawing.Point(13, 13);
this.buttonPanel.Name = "buttonPanel";
this.buttonPanel.Size = new System.Drawing.Size(200, 258);
this.buttonPanel.TabIndex = 1;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.AutoScroll = true;
this.ClientSize = new System.Drawing.Size(1008, 601);
this.Controls.Add(this.rightInnerPanel);
this.Controls.Add(this.buttonPanel);
this.Controls.Add(this.leftInnerPanel);
this.Name = "Form1";
this.Text = "Form1";
this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
this.SizeChanged += new System.EventHandler(this.Form1_SizeChanged);
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Panel rightInnerPanel;
private System.Windows.Forms.Panel leftInnerPanel;
private System.Windows.Forms.Panel buttonPanel;
}
}
You are not accounting for how much the form has scrolled, you are simply plopping the panel at the top of the visible client area.
Change your Vertical() method thusly:
private void vertical()
{
leftInnerPanel.Location = new Point(this.Width / 2 - leftInnerPanel.Width / 2, 5 - VerticalScroll.Value);
if (leftInnerPanel.Location.X <= buttonPanel.Location.X + buttonPanel.Width)
leftInnerPanel.Location = new Point(buttonPanel.Location.X + buttonPanel.Width, leftInnerPanel.Location.Y);
rightInnerPanel.Location = new Point(leftInnerPanel.Location.X, leftInnerPanel.Bottom + 10);
}
Two points to note:
1) use of the VerticalScroll member.
2) use of Bottom instead of Height (and Right instead of Width in the Horizontal() method).
In vertical()
since you are not adjusting the Y of leftInnerPanel, we can straightaway use the buttonPanel.Top
leftInnerPanel.Location = new Point(this.Width / 2 - leftInnerPanel.Width / 2, buttonPanel.Top);
Secondly,
//rightInnerPanel.Location = new Point(leftInnerPanel.Location.X, leftInnerPanel.Height + 10);
rightInnerPanel.Location = new Point(leftInnerPanel.Location.X, leftInnerPanel.Bottom + 10);

C# : How to add both scrollbars in a PictureBox contained in a SplitContainer Panel

I've found a similar question, but the answer only helps making one of the scrollbars appear.
My form has a SplitContainer. In one of its panels (Panel2) there is a PictureBox with an image i want to show.
What i need is to find a way to make both vertical and horizontal scrollbars appear when the image exceeds the panel's size.
I already set AutoScroll to true, and I've seen it's necessary to set Anchor at left for one of the scrollbars to appear, and at Top for the other one, but i need both of them.
Thanks in advance.
make sure your SplitContainer.Panel in which you embedded your PictureBox
has AutoScroll property set to True
and your PictureBox has SizeMode property set to 'AutoSize' (PictureBoxSizeMode.AutoSize).
Hope I helped you :)
I didn't find any issues setting up something like this. Here's my designer code:
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.splitContainer1 = new System.Windows.Forms.SplitContainer();
this.pictureBox1 = new System.Windows.Forms.PictureBox();
((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).BeginInit();
this.splitContainer1.Panel2.SuspendLayout();
this.splitContainer1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
this.SuspendLayout();
//
// splitContainer1
//
this.splitContainer1.Dock = System.Windows.Forms.DockStyle.Fill;
this.splitContainer1.Location = new System.Drawing.Point(0, 0);
this.splitContainer1.Name = "splitContainer1";
//
// splitContainer1.Panel2
//
this.splitContainer1.Panel2.AutoScroll = true;
this.splitContainer1.Panel2.Controls.Add(this.pictureBox1);
this.splitContainer1.Size = new System.Drawing.Size(576, 406);
this.splitContainer1.SplitterDistance = 192;
this.splitContainer1.TabIndex = 0;
//
// pictureBox1
//
this.pictureBox1.Image = global::WinFormsScratch.Properties.Resources.Desert;
this.pictureBox1.Location = new System.Drawing.Point(0, 0);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(1024, 768);
this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
this.pictureBox1.TabIndex = 0;
this.pictureBox1.TabStop = false;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(576, 406);
this.Controls.Add(this.splitContainer1);
this.Name = "Form1";
this.Text = "Form1";
this.splitContainer1.Panel2.ResumeLayout(false);
this.splitContainer1.Panel2.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).EndInit();
this.splitContainer1.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.SplitContainer splitContainer1;
private System.Windows.Forms.PictureBox pictureBox1;
}

WinForm Bottom Margin Property Doesn't Do Anything

I am new to WinForms, and I am very used to styling in CSS, so maybe I am not looking at WinForm's Bottom Margin property correctly, but, no matter what element I set an arbitrarily large bottom-margin number to, it seems to have no effect, at all.
What I want is to extend the design of the form to below the initial viewable window (vertical-scroll bar shows up just fine), and set a bottom-margin to these elements so that the very bottom of the element isn't flush with the very bottom of the window (a little space would be nice).
I have tried this on several elements just to see if it was only the one element (or the fact that it was out of the initial visible part of the window) that was giving me problems, but I can't seem to get any effect out of the margin property at all.
Looking here: http://msdn.microsoft.com/en-us/library/ms229627.aspx It seems that this is, indeed, what the margin property should do. Also, I can't find any padding within the GUI controls for any element.
As it stands, I am mostly only coding C# for the event handlers, until I get a better grasp of where Visual Studio puts everything within the two partial classes (and the other .cs files).
If it helps, here is the code for the designer file:
namespace WindowsFormsApplication1
{
partial class IntroForm
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.WelcomeHeader = new System.Windows.Forms.Label();
this.ActionSelect = new System.Windows.Forms.ComboBox();
this.ProceedBtn = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.richTextBox1 = new System.Windows.Forms.RichTextBox();
this.SuspendLayout();
//
// WelcomeHeader
//
this.WelcomeHeader.AutoSize = true;
this.WelcomeHeader.Font = new System.Drawing.Font("Microsoft Sans Serif", 15.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.WelcomeHeader.Location = new System.Drawing.Point(84, 30);
this.WelcomeHeader.Name = "WelcomeHeader";
this.WelcomeHeader.Size = new System.Drawing.Size(367, 25);
this.WelcomeHeader.TabIndex = 0;
this.WelcomeHeader.Text = "Please Select Which Content You";
//
// ActionSelect
//
this.ActionSelect.ForeColor = System.Drawing.SystemColors.WindowFrame;
this.ActionSelect.FormattingEnabled = true;
this.ActionSelect.Items.AddRange(new object[] {
"Events",
"Headline News",
"Images For Slideshow",
"Agendas",
"Job Opportunities",
"Schedule Of Meetings",
"Legal Notices",
"Main Street (Main Link)",
"Tourism (Main Link)",
"Rental Properties",
"Concert In The Park",
"Main Street News Letters"});
this.ActionSelect.Location = new System.Drawing.Point(126, 116);
this.ActionSelect.Name = "ActionSelect";
this.ActionSelect.Size = new System.Drawing.Size(283, 28);
this.ActionSelect.TabIndex = 1;
this.ActionSelect.Text = "Please Select";
//
// ProceedBtn
//
this.ProceedBtn.BackColor = System.Drawing.SystemColors.ButtonFace;
this.ProceedBtn.Cursor = System.Windows.Forms.Cursors.Hand;
this.ProceedBtn.ForeColor = System.Drawing.Color.DimGray;
this.ProceedBtn.Location = new System.Drawing.Point(221, 192);
this.ProceedBtn.Name = "ProceedBtn";
this.ProceedBtn.Size = new System.Drawing.Size(93, 34);
this.ProceedBtn.TabIndex = 2;
this.ProceedBtn.Text = "Proceed";
this.ProceedBtn.UseVisualStyleBackColor = false;
this.ProceedBtn.Click += new System.EventHandler(this.ProceedBtn_Click);
//
// label1
//
this.label1.AutoSize = true;
this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 15.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.label1.Location = new System.Drawing.Point(142, 55);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(251, 25);
this.label1.TabIndex = 3;
this.label1.Text = "Would Like To Change";
//
// richTextBox1
//
this.richTextBox1.Location = new System.Drawing.Point(147, 268);
this.richTextBox1.Margin = new System.Windows.Forms.Padding(3, 3, 3, 30);
this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.Size = new System.Drawing.Size(246, 269);
this.richTextBox1.TabIndex = 4;
this.richTextBox1.Text = "";
//
// IntroForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 20F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.AutoScroll = true;
this.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(102)))), ((int)(((byte)(0)))));
this.ClientSize = new System.Drawing.Size(535, 306);
this.Controls.Add(this.richTextBox1);
this.Controls.Add(this.label1);
this.Controls.Add(this.ProceedBtn);
this.Controls.Add(this.ActionSelect);
this.Controls.Add(this.WelcomeHeader);
this.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(204)))), ((int)(((byte)(153)))), ((int)(((byte)(0)))));
this.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5);
this.Name = "IntroForm";
this.Text = "Okmulgee Online Web File Generator";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Label WelcomeHeader;
private System.Windows.Forms.ComboBox ActionSelect;
private System.Windows.Forms.Button ProceedBtn;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.RichTextBox richTextBox1;
}
}
------------------------------------------UPDATE-------------------------------------------
The element I have set the bottom margin property to within the code above is simply, richTextBox1.
Also, I did find a Padding element to the main form element, but sadly, this doesn't push other elements away from its edges either :(
What do these properties do (margin, padding)?
The Margin property is used by the automatic layout feature built into Winforms. But it does require that you allow the container to grow so it can provide the requested margin. So you must set the form's AutoSize property to True.
Combining AutoSize and AutoScroll is possible, you can set the MaximumSize property to prevent it from growing too much. The scrollbar automatically appears when the layout calculation produces a layout that exceeds the MaximumSize. The default maximum size is the Screen.WorkingArea on which the form is displayed, usually good enough.

keep button position on panel resize

I've been messing around with something that should be simple.
I've moved jobs and trying to get some of the basic tools I have used before, but of course I don't have the old source to look at.
We had extended panel to have some standard properties and functions (save, close, save and close).
But I can't get the buttons to be positioned correctly on a resize. I put this ExtPanel on a form but the buttons keep disappearing as I resize, or don't move as expected (frozen on bottom right).
The class
public partial class ExtPanel: UserControl
{
private System.Windows.Forms.Button btnSaveandClose;
private System.Windows.Forms.Button btnCancel;
private System.Windows.Forms.Button btnSave;
public ExtPanel ()
{
InitializeComponent ();
}
// misc things this class does...
}
public partial class ExtPanel
{
private void InitializeComponent ()
{
this.btnSaveandClose = new System.Windows.Forms.Button();
this.btnCancel = new System.Windows.Forms.Button();
this.btnSave = new System.Windows.Forms.Button();
this.panel1 = new System.Windows.Forms.Panel();
this.panel1.SuspendLayout();
this.SuspendLayout();
//
// btnSaveandClose
//
this.btnSaveandClose.Location = new System.Drawing.Point(899, 689);
this.btnSaveandClose.Name = "btnSaveandClose";
this.btnSaveandClose.Size = new System.Drawing.Size(100, 30);
this.btnSaveandClose.TabIndex = 0;
this.btnSaveandClose.Text = "Save and Close";
this.btnSaveandClose.UseVisualStyleBackColor = true;
this.btnSaveandClose.Click += new System.EventHandler(this.Click_SaveandClose);
this.btnSaveandClose.Anchor = ((System.Windows.Forms.AnchorStyles) ((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
//
// btnCancel
//
this.btnCancel.Location = new System.Drawing.Point(687, 689);
this.btnCancel.Name = "btnCancel";
this.btnCancel.Size = new System.Drawing.Size(100, 30);
this.btnCancel.TabIndex = 1;
this.btnCancel.Text = "Cancel";
this.btnCancel.UseVisualStyleBackColor = true;
this.btnCancel.Click += new System.EventHandler(this.Click_Close);
this.btnCancel.Anchor = ((System.Windows.Forms.AnchorStyles) ((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
//
// btnSave
//
this.btnSave.Location = new System.Drawing.Point(793, 689);
this.btnSave.Name = "btnSave";
this.btnSave.Size = new System.Drawing.Size(100, 30);
this.btnSave.TabIndex = 2;
this.btnSave.Text = "Save";
this.btnSave.UseVisualStyleBackColor = true;
this.btnSave.Click += new System.EventHandler(this.Click_Save);
this.btnSave.Anchor = ((System.Windows.Forms.AnchorStyles) ((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
//
// panel1
//
this.panel1.AutoScroll = true;
this.panel1.Controls.Add(this.btnSave);
this.panel1.Controls.Add(this.btnCancel);
this.panel1.Controls.Add(this.btnSaveandClose);
this.panel1.Location = new System.Drawing.Point(0, 0);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(1008, 730);
this.panel1.TabIndex = 0;
//
// ExtPanel
//
this.Controls.Add(this.panel1);
this.Name = "ExtPanel";
this.Size = this.panel1.Size;
this.Click += new System.EventHandler(this.click_this);
this.panel1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Panel panel1;
}
Have you tried anchoring the button?
...I missed the anchoring code.
If you have a button in the lower right-hand corner that you want to stay in the lower right-hand corner when resizing the form, set it's anchor properties to Bottom, Right.
Update:
I loaded your code. You have a panel inside of ExtPanel. If you dock (Fill) that panel then you should be working fine by resizing ExtPanel.

Categories