【问题标题】:Relocating panels gets mixed up WinForms重新定位面板混淆了 WinForms
【发布时间】:2014-09-30 19:14:56
【问题描述】:

我正在使用 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;
    }
}

【问题讨论】:

  • 使用SplitContainer调整它的方向不是更容易吗?
  • 我不熟悉SplitContainers。我尝试使用有助于定位内部面板(两个大面板)的容器面板,但仍然搞砸了
  • SplitContainer 将调整大小(除非它是固定的)并将其方向从垂直更改为水平,您将更改第二个面板位置(右侧或底部)。
  • 您也可以使用table layout panel 并按百分比设置面板的宽度和高度,因为您可以在每个单元格中添加一个控件,所以不要忘记在每个单元格中先放置一个面板然后添加控件在那个面板上
  • 问题可能是您在vertical()/horizontal() funcs 中计算leftInnerPanel.Location,并结合滚动条打开。检查逻辑 - 可以做到。

标签: c# winforms


【解决方案1】:

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

因此更改您的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) 在Horizontal() 方法中使用Bottom 代替Height(和Right 代替Width)。

【讨论】:

  • 谢谢先生给我一个实际的答案,而不是建议我使用其他方法
【解决方案2】:

vertical() 由于您没有调整leftInnerPanelY,我们可以直接使用buttonPanel.Top

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

其次,

//rightInnerPanel.Location = new Point(leftInnerPanel.Location.X, leftInnerPanel.Height + 10);
rightInnerPanel.Location = new Point(leftInnerPanel.Location.X, leftInnerPanel.Bottom + 10);

【讨论】:

  • 谢谢先生给我一个实际的答案,而不是建议我使用其他方法。尽管您的方法似乎不像其他答案那样有效,但我仍然给您一个赞成票,因为大多数时候在这个网站上,答案告诉我使用另一种方法,我不喜欢您使用的方法
  • 谢谢,我同意@DonBoitnott's 应该是一个更好的答案,虽然我没有尝试过。关于使用其他方法的建议:有时我们会得到完全不同的方向,这就是论坛的用途;) - 冷静点。欢呼
  • 我不介意建议,我介意的只是获得建议而不是实际的解决方案。:P anw
猜你喜欢
  • 2014-07-15
  • 1970-01-01
  • 1970-01-01
  • 2015-03-22
  • 2021-03-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多