C# 滚动TableLayoutPanel时如何防止撕裂?

C# 滚动TableLayoutPanel时如何防止撕裂?,c#,winforms,c#-2.0,tablelayoutpanel,C#,Winforms,C# 2.0,Tablelayoutpanel,我认为垂直撕裂是描述我所看到的东西的恰当术语,但这里有一个屏幕截图显示了问题: 我认为属性可以帮助实现这一点,但它在我的窗体上设置它或在构造函数中继承和设置它似乎都没有任何效果 我为下面的长代码块感到抱歉,但我觉得应该包括一个完整的示例来演示这个问题。您应该能够复制它并运行它来复制我的问题: public class ScrollTearingDemo : Form { private const int ROW_COUNT = 20; /// <summary>

我认为垂直撕裂是描述我所看到的东西的恰当术语,但这里有一个屏幕截图显示了问题:

我认为属性可以帮助实现这一点,但它在我的窗体上设置它或在构造函数中继承和设置它似乎都没有任何效果

我为下面的长代码块感到抱歉,但我觉得应该包括一个完整的示例来演示这个问题。您应该能够复制它并运行它来复制我的问题:

public class ScrollTearingDemo : Form
{
    private const int ROW_COUNT = 20;
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new ScrollTearingDemo());
    }

    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.IContainer components = null;
    private System.Windows.Forms.TableLayoutPanel tableLayoutPanel1;

    public ScrollTearingDemo()
    {
        InitializeComponent();
        this.initializeTable();
    }

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

    // Moved this here to encapsulate demo in single source file
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
        this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel();
        //this.tableLayoutPanel1 = new BufferedTableLayoutPanel();
        this.SuspendLayout();
        // 
        // tableLayoutPanel1
        // 
        this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel();
        this.tableLayoutPanel1.AutoScroll = true;
        this.tableLayoutPanel1.CellBorderStyle = System.Windows.Forms.TableLayoutPanelCellBorderStyle.Single;
        this.tableLayoutPanel1.ColumnCount = 2;
        this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle());
        this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle());
        this.tableLayoutPanel1.Dock = DockStyle.Fill;
        this.tableLayoutPanel1.Location = new System.Drawing.Point(0, 0);
        this.tableLayoutPanel1.Name = "tableLayoutPanel1";
        this.tableLayoutPanel1.TabIndex = 0;
        // 
        // ScrollTearingDemo
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(284, 262);
        this.Controls.Add(this.tableLayoutPanel1);
        this.DoubleBuffered = true;
        this.Name = "ScrollTearingDemo";
        this.Text = "ScrollTearingDemo";
        this.ResumeLayout(false);

    }

    private void initializeTable()
    {
        // There is one more empty row to take up any extra space
        // in the event the number of rows does not fill the table.
        this.tableLayoutPanel1.RowCount = ROW_COUNT + 1;

        for(int j = 0; j < ROW_COUNT;j++)
        {
            Label markerLabel = new Label();
            markerLabel.Dock = System.Windows.Forms.DockStyle.Fill;
            markerLabel.TextAlign = ContentAlignment.MiddleRight;
            markerLabel.Name = "Label " + j;
            markerLabel.Text = markerLabel.Name;

            TextBox inputItem = new TextBox();
            inputItem.Dock = DockStyle.Fill;
            inputItem.Name = "Input " + j;
            inputItem.Text = inputItem.Name;
            inputItem.TextAlign = HorizontalAlignment.Right;
            inputItem.CausesValidation = true;

            this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.AutoSize, 25F));

            this.tableLayoutPanel1.Controls.Add(markerLabel, 0, j);
            this.tableLayoutPanel1.Controls.Add(inputItem, 1, j);
        }

        // Row style for the empty filler row.
        this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 0F));

        this.ResumeLayout();
    }
}
公共类ScrollTearingDemo:表单
{
私有常量int ROW_COUNT=20;
/// 
///应用程序的主要入口点。
/// 
[状态线程]
静态void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
运行(新的ScrollTearingDemo());
}
/// 
///必需的设计器变量。
/// 
private System.ComponentModel.IContainer components=null;
private System.Windows.Forms.TableLayoutPanel TableLayoutPanel 1;
公共ScrollTearingDemo()
{
初始化组件();
this.initializeTable();
}
/// 
///清理所有正在使用的资源。
/// 
///如果应释放托管资源,则为true;否则为false。
受保护的覆盖无效处置(布尔处置)
{
if(处理和(组件!=null))
{
组件。Dispose();
}
基地。处置(处置);
}
//将此移到此处以将演示封装在单个源文件中
/// 
///设计器支持所需的方法-不修改
///此方法的内容与代码编辑器一起使用。
/// 
私有void InitializeComponent()
{
this.TableLayoutPanel 1=新系统.Windows.Forms.TableLayoutPanel();
//this.TableLayoutPanel 1=新的BufferedTableLayoutPanel();
这个.SuspendLayout();
// 
//tableLayoutPanel1
// 
this.TableLayoutPanel 1=新系统.Windows.Forms.TableLayoutPanel();
this.tableLayoutPanel1.AutoScroll=true;
this.tableLayoutPanel1.CellBorderStyle=System.Windows.Forms.TableLayoutPanelCellBorderStyle.Single;
this.tableLayoutPanel1.ColumnCount=2;
this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle());
this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle());
this.tableLayoutPanel1.Dock=DockStyle.Fill;
this.tableLayoutPanel1.Location=新系统.Drawing.Point(0,0);
this.tableLayoutPanel1.Name=“tableLayoutPanel1”;
this.tableLayoutPanel1.TabIndex=0;
// 
//滚动撕裂演示
// 
此.AutoScaleDimensions=新系统.Drawing.SizeF(6F,13F);
this.AutoScaleMode=System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize=新系统.Drawing.Size(284262);
this.Controls.Add(this.tableLayoutPanel1);
this.DoubleBuffered=true;
this.Name=“ScrollTearingDemo”;
this.Text=“ScrollTearingDemo”;
此选项为.resume布局(false);
}
private void initializeTable()
{
//还有一个空行要占用额外的空间
//在这种情况下,行数不能填充表格。
this.tableLayoutPanel1.RowCount=行数+1;
对于(int j=0;j

这个项目仅限于C#2.0。

TableLayoutPanel是一个非常邪恶的控件。它使程序员相信他们可以使用它来实现自己的网格控制。如果不能,请改用DataGridView。绘制工件是由“滚动时显示窗口内容”选项引起的。这是一个系统选项,因此您无法合理地将其关闭。中的解决方法解决了您的问题。@HansPassant是的,现在我每次尝试使用它时都会考虑它,但最终由于某种原因不得不使用其他东西。在我意识到这口井有毒之前,我要试水多少次?解决方法现在已经完成了工作,但我可能会接受您的建议,并切换到DataGridView。