C# Win Forms 制作可调整大小的控件

5
我有一个面板里面有两个控件。我希望它们紧贴着面板的边缘(面板有一定的宽度和高度不能改变),但是可以在垂直方向上调整他们(控件)所获得的面板空间大小。
panel.Controls.Add(listview1);
panel.Controls.Add(listview2);

有两个列表视图一个接一个地放置(垂直)。 我希望有可能通过选择它们之间的边框来“更改高度”以进行调整。

我希望您能理解我的意思。 预先感谢您的帮助。


2
你想要的是能够更改每个ListView在其父容器内占用的区域,但它们共享一个边框,以便当一个增长时,另一个会缩小以占用剩余空间?(仅为了其他用户的清晰度) - RhysW
你可以查看WinForms中的TableLayoutPanel控件,例如用它来创建一些网格。链接:http://msdn.microsoft.com/en-us/library/h21wykkx.aspx 和 http://msdn.microsoft.com/en-us/library/system.windows.forms.tablelayoutpanel.aspx - Styxxy
您是否想让用户通过拖动边缘手动更改控件的大小? - nawfal
5个回答

7

2

2

将上面的文档属性设置为顶部。 在同一容器(面板)中添加一个方向为垂直的分隔栏。将下面的 Dock 属性设置为 fill。 这是一种方法。


2
我同意Paul的观点,SplitContainer 是你要找的。我想补充一点,你需要设置放置在分割容器内的控件的 Dock 和 Anchor 属性。如果你将子控件的 Dock 属性设置为 Fill,它会扩展到填满整个容器,无论面板的大小如何。如果面板中有多个控件,则使用 Anchor 属性。在这种情况下,你可以设置子控件的 Anchor 属性来告诉它哪些边缘“粘”在容器的边缘上。请参阅 this page 以了解更全面的信息。
此外,您需要设置SplitContainer控件本身的DockAnchor属性。这将使其在窗体调整大小时进行调整。然后,在SplitContainer内部的子控件上设置Anchor/Dock属性将导致子控件随容器调整大小。

1
你有考虑在ListView上使用Anchor吗?
        this.panel1 = new System.Windows.Forms.Panel();
        this.listView1 = new System.Windows.Forms.ListView();
        this.listView2 = new System.Windows.Forms.ListView();
        this.panel1.SuspendLayout();
        this.SuspendLayout();
        // 
        // panel1
        // 
        this.panel1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
        | System.Windows.Forms.AnchorStyles.Left) 
        | System.Windows.Forms.AnchorStyles.Right)));
        this.panel1.Controls.Add(this.listView2);
        this.panel1.Controls.Add(this.listView1);
        this.panel1.Location = new System.Drawing.Point(12, 12);
        this.panel1.Name = "panel1";
        this.panel1.Size = new System.Drawing.Size(413, 280);
        this.panel1.TabIndex = 0;
        // 
        // listView1
        // 
        this.listView1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
        | System.Windows.Forms.AnchorStyles.Right)));
        this.listView1.Location = new System.Drawing.Point(3, 0);
        this.listView1.Name = "listView1";
        this.listView1.Size = new System.Drawing.Size(410, 97);
        this.listView1.TabIndex = 0;
        this.listView1.UseCompatibleStateImageBehavior = false;
        // 
        // listView2
        // 
        this.listView2.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left) 
        | System.Windows.Forms.AnchorStyles.Right)));
        this.listView2.Location = new System.Drawing.Point(0, 183);
        this.listView2.Name = "listView2";
        this.listView2.Size = new System.Drawing.Size(410, 97);
        this.listView2.TabIndex = 1;
        this.listView2.UseCompatibleStateImageBehavior = false;
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(437, 304);
        this.Controls.Add(this.panel1);
        this.Name = "Form1";
        this.Text = "Form1";
        this.panel1.ResumeLayout(false);
        this.ResumeLayout(false);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接