FlowLayoutPanel

时间:2016-12-13 09:28:06

标签: c# winforms

我试图让我的自定义控件以某种方式自动适应水平边缘的流布局面板。

我想要完成的是显示自定义控件的垂直列表。但我得到的是:

Screenshot of VS Designer

我的自定义控件的代码是:

namespace CustomControl
{
    partial class ChatMessage
    {
        /// <summary> 
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

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

        #region Component Designer generated code

        /// <summary> 
        /// Required method for Designer support - do not modify 
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(ChatMessage));
            this.ContentsTable = new System.Windows.Forms.TableLayoutPanel();
            this.DateTimeLabel = new System.Windows.Forms.Label();
            this.NumberLabel = new System.Windows.Forms.Label();
            this.NameLabel = new System.Windows.Forms.Label();
            this.MessageLabel = new System.Windows.Forms.Label();
            this.DirectionLabel = new System.Windows.Forms.Label();
            this.ContentsTable.SuspendLayout();
            this.SuspendLayout();
            // 
            // ContentsTable
            // 
            this.ContentsTable.AutoSize = true;
            this.ContentsTable.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
            this.ContentsTable.BackColor = System.Drawing.Color.White;
            this.ContentsTable.ColumnCount = 2;
            this.ContentsTable.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.ContentsTable.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.ContentsTable.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 20F));
            this.ContentsTable.Controls.Add(this.DateTimeLabel, 1, 2);
            this.ContentsTable.Controls.Add(this.NumberLabel, 1, 0);
            this.ContentsTable.Controls.Add(this.NameLabel, 0, 0);
            this.ContentsTable.Controls.Add(this.MessageLabel, 0, 1);
            this.ContentsTable.Controls.Add(this.DirectionLabel, 0, 2);
            this.ContentsTable.Dock = System.Windows.Forms.DockStyle.Fill;
            this.ContentsTable.Location = new System.Drawing.Point(48, 0);
            this.ContentsTable.Name = "ContentsTable";
            this.ContentsTable.RowCount = 3;
            this.ContentsTable.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 20F));
            this.ContentsTable.RowStyles.Add(new System.Windows.Forms.RowStyle());
            this.ContentsTable.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 20F));
            this.ContentsTable.Size = new System.Drawing.Size(592, 54);
            this.ContentsTable.TabIndex = 0;
            // 
            // DateTimeLabel
            // 
            this.DateTimeLabel.Dock = System.Windows.Forms.DockStyle.Fill;
            this.DateTimeLabel.Font = new System.Drawing.Font("Calibri", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.DateTimeLabel.ForeColor = System.Drawing.SystemColors.ControlDark;
            this.DateTimeLabel.Location = new System.Drawing.Point(299, 104);
            this.DateTimeLabel.Name = "DateTimeLabel";
            this.DateTimeLabel.Size = new System.Drawing.Size(290, 20);
            this.DateTimeLabel.TabIndex = 0;
            this.DateTimeLabel.Text = "Date / Time";
            this.DateTimeLabel.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // NumberLabel
            // 
            this.NumberLabel.Dock = System.Windows.Forms.DockStyle.Fill;
            this.NumberLabel.Font = new System.Drawing.Font("Calibri", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.NumberLabel.Location = new System.Drawing.Point(299, 0);
            this.NumberLabel.Name = "NumberLabel";
            this.NumberLabel.Size = new System.Drawing.Size(290, 20);
            this.NumberLabel.TabIndex = 1;
            this.NumberLabel.Text = "Number or ID";
            this.NumberLabel.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // NameLabel
            // 
            this.NameLabel.Dock = System.Windows.Forms.DockStyle.Fill;
            this.NameLabel.Font = new System.Drawing.Font("Calibri", 10F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.NameLabel.Location = new System.Drawing.Point(3, 0);
            this.NameLabel.Name = "NameLabel";
            this.NameLabel.Size = new System.Drawing.Size(290, 20);
            this.NameLabel.TabIndex = 2;
            this.NameLabel.Text = "Name";
            this.NameLabel.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            // 
            // MessageLabel
            // 
            this.MessageLabel.AutoSize = true;
            this.ContentsTable.SetColumnSpan(this.MessageLabel, 2);
            this.MessageLabel.Dock = System.Windows.Forms.DockStyle.Fill;
            this.MessageLabel.Font = new System.Drawing.Font("Calibri", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.MessageLabel.Location = new System.Drawing.Point(3, 20);
            this.MessageLabel.Name = "MessageLabel";
            this.MessageLabel.Size = new System.Drawing.Size(586, 84);
            this.MessageLabel.TabIndex = 3;
            this.MessageLabel.Text = resources.GetString("MessageLabel.Text");
            // 
            // DirectionLabel
            // 
            this.DirectionLabel.Dock = System.Windows.Forms.DockStyle.Fill;
            this.DirectionLabel.ForeColor = System.Drawing.Color.LightGray;
            this.DirectionLabel.Location = new System.Drawing.Point(3, 104);
            this.DirectionLabel.Name = "DirectionLabel";
            this.DirectionLabel.Size = new System.Drawing.Size(290, 20);
            this.DirectionLabel.TabIndex = 4;
            this.DirectionLabel.Text = "Direction";
            this.DirectionLabel.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            // 
            // ChatMessage
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.AutoSize = true;
            this.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
            this.Controls.Add(this.ContentsTable);
            this.MinimumSize = new System.Drawing.Size(320, 0);
            this.Name = "ChatMessage";
            this.Padding = new System.Windows.Forms.Padding(48, 0, 0, 0);
            this.Size = new System.Drawing.Size(640, 54);
            this.ContentsTable.ResumeLayout(false);
            this.ContentsTable.PerformLayout();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.TableLayoutPanel ContentsTable;
        private System.Windows.Forms.Label DateTimeLabel;
        private System.Windows.Forms.Label NumberLabel;
        private System.Windows.Forms.Label NameLabel;
        private System.Windows.Forms.Label MessageLabel;
        private System.Windows.Forms.Label DirectionLabel;
    }
}

您可以看到我的自定义控件溢出表单大小。 注意:图片中使用的流程布局面板有一个底座填充,流向=自上而下&amp; wrap contents = false。

有人可以建议吗?

1 个答案:

答案 0 :(得分:0)

使用this page中的自定义标签类作为@Reza Aghaei建议的面板内的邮件标签完成了这项工作。看到下面的结果,这正是我想要的。

enter image description here