【问题标题】:Panel in a panel, autoscroll面板中的面板,自动滚动
【发布时间】:2013-02-05 17:47:35
【问题描述】:

在我的真实程序遇到问题后,我正在使用 WinForms 做一个极简测试应用程序。我把一个小面板(孩子)放在一个更大的面板(父母)里面。较大的面板将 AutoScroll 设置为 true。子面板的默认锚点设置为顶部和左侧。子面板未停靠。

我想要的行为是让滚动条在较小面板的位置过于偏移时出现,无论是顶部、底部、左侧还是右侧。问题是它仅在它太靠右或底部太远时才有效。顶部过多或左侧过多时不会出现滚动条。

我使用两个简单的按钮将子面板的位置强制向左移动 200 像素,或向右移动 200 像素,以便快速轻松地修改其位置。

这是我的 Form1() 代码:

        private void button1_Click(object sender, EventArgs e)
    {
        childPanel.Location = new Point(childPanel.Location.X - 200, childPanel.Location.Y);
        hostPanel.Invalidate();
    }

    private void button2_Click(object sender, EventArgs e)
    {
        childPanel.Location = new Point(childPanel.Location.X + 200, childPanel.Location.Y);
        hostPanel.Invalidate();
    }

这是设计器代码:

       private void InitializeComponent()
    {
        this.hostPanel = new System.Windows.Forms.Panel();
        this.childPanel = new System.Windows.Forms.Panel();
        this.moveChildLeft = new System.Windows.Forms.Button();
        this.moveChildRight = new System.Windows.Forms.Button();
        this.hostPanel.SuspendLayout();
        this.SuspendLayout();
        // 
        // hostPanel
        // 
        this.hostPanel.AutoScroll = true;
        this.hostPanel.BackColor = System.Drawing.SystemColors.AppWorkspace;
        this.hostPanel.Controls.Add(this.childPanel);
        this.hostPanel.Location = new System.Drawing.Point(239, 48);
        this.hostPanel.Name = "hostPanel";
        this.hostPanel.Size = new System.Drawing.Size(400, 400);
        this.hostPanel.TabIndex = 0;
        // 
        // childPanel
        // 
        this.childPanel.BackColor = System.Drawing.SystemColors.ButtonHighlight;
        this.childPanel.Location = new System.Drawing.Point(29, 62);
        this.childPanel.Name = "childPanel";
        this.childPanel.Size = new System.Drawing.Size(342, 259);
        this.childPanel.TabIndex = 0;
        // 
        // moveChildLeft
        // 
        this.moveChildLeft.Location = new System.Drawing.Point(61, 81);
        this.moveChildLeft.Name = "moveChildLeft";
        this.moveChildLeft.Size = new System.Drawing.Size(75, 23);
        this.moveChildLeft.TabIndex = 1;
        this.moveChildLeft.Text = "Left 200";
        this.moveChildLeft.UseVisualStyleBackColor = true;
        this.moveChildLeft.Click += new System.EventHandler(this.button1_Click);
        // 
        // moveChildRight
        // 
        this.moveChildRight.Location = new System.Drawing.Point(61, 111);
        this.moveChildRight.Name = "moveChildRight";
        this.moveChildRight.Size = new System.Drawing.Size(75, 23);
        this.moveChildRight.TabIndex = 2;
        this.moveChildRight.Text = "Right 200";
        this.moveChildRight.UseVisualStyleBackColor = true;
        this.moveChildRight.Click += new System.EventHandler(this.button2_Click);
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(1018, 549);
        this.Controls.Add(this.moveChildRight);
        this.Controls.Add(this.moveChildLeft);
        this.Controls.Add(this.hostPanel);
        this.Name = "Form1";
        this.Text = "Form1";
        this.hostPanel.ResumeLayout(false);
        this.ResumeLayout(false);
    }

【问题讨论】:

  • 如果锚定在上面或左边怎么可能太多?
  • 如果我删除子面板的锚属性(即我将其设置为无),那么无论子面板的位置如何,主机面板中都绝对不会出现滚动条。自动滚动条似乎取决于某处至少存在 1 个锚点。

标签: c# winforms panel autoscroll


【解决方案1】:

Yet - Another WPF 迅速解决了 Winforms 无法使用的问题:

XAML:

<Window x:Class="WpfApplication4.Window3"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window3" WindowState="Maximized">
    <DockPanel>
        <StackPanel DockPanel.Dock="Top" Orientation="Horizontal">
            <Button Content="Left" Click="MoveLeft"/>
            <Button Content="Right" Click="MoveRight"/>
        </StackPanel>
        <Border BorderBrush="Blue" BorderThickness="1" Width="300" Height="300">
            <ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto" x:Name="Scr">
                <Grid Background="Green" Width="100" Height="100" x:Name="Grid"/>
            </ScrollViewer>
        </Border>
    </DockPanel>
</Window>

后面的代码:

using System.Windows;

namespace WpfApplication4
{
    public partial class Window3 : Window
    {
        public Window3()
        {
            InitializeComponent();
        }

        private void MoveRight(object sender, RoutedEventArgs e)
        {
            if (Grid.Margin.Right <= 0)
            {
                Grid.Margin = new Thickness(Grid.Margin.Left + 100,0,0,0);
            }
            else
            {
                Grid.Margin = new Thickness(0, 0, Grid.Margin.Right - 100, 0);
                Scr.ScrollToHorizontalOffset(Scr.HorizontalOffset - 100);
            }
        }

        private void MoveLeft(object sender, RoutedEventArgs e)
        {
            if (Grid.Margin.Left > 0)
            {
                Grid.Margin = new Thickness(Grid.Margin.Left - 100, 0, 0, 0);
            }
            else
            {
                Grid.Margin = new Thickness(0, 0, Grid.Margin.Right + 100, 0);
                Scr.ScrollToHorizontalOffset(Scr.HorizontalOffset + 100);
            }
        }
    }
}

将我的代码复制并粘贴到文件 -> 新建 -> WPF 应用程序中,然后自己查看结果。

【讨论】:

    【解决方案2】:

    最终,您可能希望将您的应用程序转换为 WPF。既然如此,Winform 就被判了小死。

    【讨论】:

      猜你喜欢
      • 2021-08-28
      • 1970-01-01
      • 2011-06-24
      • 1970-01-01
      • 2015-03-14
      • 2010-11-29
      • 1970-01-01
      相关资源
      最近更新 更多