重定位面板混淆了WinForms

时间:2014-09-30 10:09:07

标签: c# winforms

我正在使用C#和WinForms创建一个应用程序。我有三个面板。其中两个相当大,目前并排放置。我正在尝试为具有较小屏幕或希望将表单设置为较小尺寸的用户实现一项功能。我正在重新安置中间的右侧面板,并将它们重新定位在窗体中。 我遇到的问题是,当用户向下滚动并再次调整窗体大小时,两个中间面板(大面板)在窗体中向下移动,在顶部留下一大块空白区域。

我的代码很简单

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;
            }
        }
    }
}

我的设计师代码也相当简单。

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;
    }
}

2 个答案:

答案 0 :(得分:2)

您没有考虑表单滚动的程度,只是将面板放在可见客户区顶部。

这样改变你的Vertical()方法:

    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);
    }

需要注意的两点:

1)使用VerticalScroll成员。

2)在Bottom方法中使用Height代替Right(以及Width代替Horizontal()

答案 1 :(得分:1)

vertical() 由于您未调整Y的{​​{1}},我们可以直接使用leftInnerPanel

buttonPanel.Top

其次,

leftInnerPanel.Location = new Point(this.Width / 2 - leftInnerPanel.Width / 2, buttonPanel.Top);