Reputation: 4377
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;
}
}
Upvotes: 1
Views: 107
Reputation: 1152
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);
Upvotes: 1
Reputation: 11025
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).
Upvotes: 2