滚动TableLayoutPanel时如何防止撕裂?

时间:2014-07-16 14:46:46

标签: c# winforms c#-2.0 tablelayoutpanel

我认为垂直撕裂是描述我所看到的内容的合适术语,但这里是一个显示问题的屏幕截图:

A scrolling panel with the a Label and an TextBox in a row at the top and then again below that but cut off as it degrades into a series of grey lines.

我认为DoubleBuffered属性可以帮助解决这个问题,但它既没有在我的表单上设置它,也没有继承TableLayoutPanel并在构造函数中设置它似乎有任何影响。

我为以下长代码段道歉,但我觉得我应该包含一个演示该问题的完整示例。你应该能够复制它并运行它来复制我的问题:

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

这个项目仅限于C#2.0。

0 个答案:

没有答案