调整 AutoScroll 面板的大小会影响滚动位置

Resizing of an AutoScroll Panel affects scrolled position

当我使用正确的调整大小手柄调整以下表单大小时,包含的 TableLayoutPanel 会用滚动条装饰(如预期的那样,panel1.AutoScroll = true)以获得较小的表单大小,但 TableLayoutPanel 也会从其原始位置移位.见下图:仅使用右侧调整大小手柄调整表单大小后,第二个的滚动条不在最左边,内容的左边框被切断。

似乎这种行为与嵌套 RadioButtons 的存在有关,因为如果我删除它们(或者用另一个 TextBox 替换它们),"normal" 行为就会恢复(TableLayoutPanel 保持原位在调整大小时)。

我必须设置哪些属性才能使内容相对于(上)左边框始终保持静止?

顺便说一句:当我用 TabControl + 一个 TabPage 替换 panel1 时,"normal" 行为也恢复了。

using System;
using System.Drawing;
using System.Windows.Forms;

namespace Test
{
    /// <summary>
    /// Description of Form3.
    /// </summary>
    public partial class Form3 : Form
    {
        const int textBoxNameWidth = 500;

        TableLayoutPanel testControl1;
        Panel panel1;
        TextBox textBoxName;
        RadioButton radioButtonNo;
        RadioButton radioButtonYes;
        TableLayoutPanel tableLayoutPanelDecision;

        public Form3()
        {
            testControl1 = new TableLayoutPanel();
            panel1 = new Panel();
            textBoxName = new TextBox();
            radioButtonNo = new RadioButton();
            radioButtonYes = new RadioButton();
            tableLayoutPanelDecision = new TableLayoutPanel();

            testControl1.AutoSize = true;
            testControl1.AutoSizeMode = AutoSizeMode.GrowAndShrink;
            testControl1.Location = new Point(0, 0);
            testControl1.Dock = DockStyle.None;
            testControl1.ColumnCount = 2;
            testControl1.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
            testControl1.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
            testControl1.RowCount = 2;
            testControl1.RowStyles.Add(new RowStyle(SizeType.AutoSize));
            testControl1.RowStyles.Add(new RowStyle(SizeType.AutoSize));
            testControl1.Controls.Add(textBoxName, 1, 0);
            testControl1.Controls.Add(tableLayoutPanelDecision, 1, 1);

            textBoxName.Text = "New Boolean";
            textBoxName.TextAlign = HorizontalAlignment.Center;
            textBoxName.Anchor = (AnchorStyles.Left | AnchorStyles.Right);
            textBoxName.TabStop = false;
            textBoxName.Width = textBoxNameWidth;

            tableLayoutPanelDecision.AutoSize = true;
            tableLayoutPanelDecision.ColumnCount = 2;
            tableLayoutPanelDecision.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 50f));
            tableLayoutPanelDecision.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 50f));
            tableLayoutPanelDecision.RowCount = 1;
            tableLayoutPanelDecision.RowStyles.Add(new RowStyle(SizeType.AutoSize));
            tableLayoutPanelDecision.Controls.Add(radioButtonYes, 0, 0);
            tableLayoutPanelDecision.Controls.Add(radioButtonNo, 1, 0);
            tableLayoutPanelDecision.Dock = DockStyle.Fill;

            radioButtonNo.Checked = true;
            radioButtonNo.AutoSize = true;
            radioButtonNo.TabIndex = 1;
            radioButtonNo.TabStop = true;
            radioButtonNo.Text = "False";
            radioButtonNo.UseVisualStyleBackColor = true;
            radioButtonNo.Anchor = AnchorStyles.None;

            radioButtonYes.AutoSize = true;
            radioButtonYes.TabIndex = 0;
            radioButtonYes.Text = "True";
            radioButtonYes.UseVisualStyleBackColor = true;
            radioButtonYes.Anchor = AnchorStyles.None;

            panel1.AutoScroll = true;
            panel1.Controls.Add(testControl1);
            panel1.Dock = System.Windows.Forms.DockStyle.Fill;
            panel1.Location = new System.Drawing.Point(0, 0);
            panel1.Name = "panel1";
            panel1.Size = new System.Drawing.Size(560, 219);
            panel1.TabIndex = 1;

            AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            ClientSize = new System.Drawing.Size(560, 219);
            Controls.Add(panel1);
            Name = "Form3";
            Text = "Form3";
        }
    }
}

面板正试图将可聚焦的控件保持在用户的视图中。要更改它,您必须使用自己的面板:

public class PanelEx : Panel {
  protected override Point ScrollToControl(Control activeControl) {
    return this.DisplayRectangle.Location;
  }
}