【问题标题】:Autoscroll property of TableLayoutPanel is not workingTableLayoutPanel 的自动滚动属性不起作用
【发布时间】:2023-03-05 05:37:01
【问题描述】:

我想在TableLayoutPanel 中动态添加行,并在 GUI 上的固定区域中。因此,如果记录数量增加,那么我想要一个垂直滚动条来帮助用户查看更多记录。为此,我设置了 PropertyAutoScroll = true; 但它不起作用。

CheckBox c = new CheckBox();
c.Text = "Han";
tableLayoutPanel1.GrowStyle = TableLayoutPanelGrowStyle.AddRows;
tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.AutoSize));
this.tableLayoutPanel1.RowCount = 1; this.tableLayoutPanel1.Controls.Add(c, 0, 0);
tableLayoutPanel1.AutoScrollPosition = new Point(0, tableLayoutPanel1.VerticalScroll.Maximum);
this.tableLayoutPanel1.AutoScroll = true;
tableLayoutPanel1.Padding = new Padding(0, 0, SystemInformation.VerticalScrollBarWidth, 0);

【问题讨论】:

    标签: c# .net winforms tablelayout


    【解决方案1】:

    从另一个问题的 cmets 中查看您的代码,您似乎在每一行都添加了行样式,请尝试在不添加样式的情况下添加行,或者先添加一种样式然后添加所有行。

      tableLayoutPanel1.GrowStyle = TableLayoutPanelGrowStyle.AddRows;
                tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.AutoSize));
    
                this.tableLayoutPanel1.Controls.Add(c);
                this.tableLayoutPanel1.Controls.Add(c1);
                this.tableLayoutPanel1.Controls.Add(c2);
    tableLayoutPanel1.VerticalScroll.Maximum = 200;
                this.tableLayoutPanel1.AutoScroll = true;
    

    【讨论】:

    • 我不得不为每一行使用不同的样式,所以不添加样式不是一种选择。在最后添加一个没有样式的行解决了这个问题
    【解决方案2】:

    因此你没有发布你的代码,我不能说你做错了什么。但这是您应该向表格布局面板添加控件的方式:

    tableLayoutPanel.GrowStyle = TableLayoutPanelGrowStyle.AddRows;
    tableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.AutoSize));
    tableLayoutPanel.RowCount = tableLayoutPanel.RowStyles.Count;
    YourCountrol control = new YourControl();
    // setup your control properties
    tableLayoutPanel.Controls.Add(control);
    // scroll to the bottom to see just added control
    tableLayoutPanel.AutoScrollPosition = 
        new Point(0, tableLayoutPanel.VerticalScroll.Maximum);
    

    当然你应该有tableLayoutPanel.AutoScroll = true

    顺便说一句,为了避免烦人的水平滚动条,您应该在表格布局面板中添加右填充:

    tableLayoutPanel.Padding = 
         new Padding(0, 0, SystemInformation.VerticalScrollBarWidth, 0);
    

    应该为 tableLayoutPanel 禁用更新 AutoSize。否则滚动将不会出现 - 表格布局面板会变大。

    【讨论】:

    • 这是我的代码,但仍然无法正常工作:CheckBox c = new CheckBox(); c.Text = "Han"; tableLayoutPanel1.GrowStyle = TableLayoutPanelGrowStyle.AddRows; tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.AutoSize)); this.tableLayoutPanel1.RowCount = 1; this.tableLayoutPanel1.Controls.Add(c, 0, 0); tableLayoutPanel1.AutoScrollPosition = new Point(0, tableLayoutPanel1.VerticalScroll.Maximum); this.tableLayoutPanel1.AutoScroll = true; tableLayoutPanel1.Padding = new Padding(0, 0, SystemInformation.VerticalScrollBarWidth, 0);
    • this.tableLayoutPanel1.RowCount = 1; 您将行数设置为一。那就是问题所在。还要在设计器中将自动滚动设置为 true。
    • 我还在 Designer Properties 中设置了 auto-scroll = true 并删除了 this.tableLayoutPanel1.RowCount = 1; 行,但仍然无法正常工作
    • @OsamaJameelAhmad 看起来您已将 AutoSize = true 设置为您的表格布局面板。应该禁用自动调整大小。
    • 注意:在 Visual Studio 2010 中,将“SystemInformation.VerticalScrollBarWidth”作为 Padding 值后,当返回设计页面时,该值在此处和 Designer.cs 中均更改为 16。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多