Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# win forms make controls resizable

I have a panel with two controls inside. I wish they were sticked to border of panel (panel has some width and height that cant be changed) but have possibility to resize the amount of space they (controls) get from panel in vertical direction.

panel.Controls.Add(listview1);
panel.Controls.Add(listview2);

Two listviews are placed one after another (vertically). I wish to have possibility to "change height" of them (by choosing border between them to resize).

I hope you understood what I mean. Thanks for help in advance.

like image 792
santBart Avatar asked May 08 '12 13:05

santBart


5 Answers

I suggest using the SplitContainer control from the Containers section of your designer toolbox.

Here's an MSDN training video on using a SplitContainer.

like image 138
Paul Sasik Avatar answered Oct 25 '22 05:10

Paul Sasik


You need to make use of Table Layout Panel to achieve that

like image 35
Tigran Avatar answered Oct 25 '22 05:10

Tigran


Set the doc property of the upper one to top. Add a Splitter bar with orientation to vertical in the same container (panel). Set the lower one's Dock property to fill. One way of doing it anyway.

like image 35
Tony Hopkinson Avatar answered Oct 25 '22 06:10

Tony Hopkinson


I agree with Paul that the SplitContainer is what you are looking for. I would add that you need to set the Dock and Anchor properties of the controls you put inside the split container. If you set a child control's Dock property to Fill, it will expand to fill up the entire container, regardless of how the panel is sized. The Anchor property is used if there are multiple controls in the panel. In that case, you set the child control's Anchor property to tell the child control which sides "stick" to the container's side. See this page for a more comprehensive look at those two properties.

Also, you will want to set the Dock or Anchor properties on the SplitContainer control itself. That will make it resize when the form resizes. Then setting the Anchor/Dock properties on the child controls inside the SplitContainer will cause the child controls to resize as the container resizes.

like image 44
Katie Kilian Avatar answered Oct 25 '22 04:10

Katie Kilian


Have you considered using Anchor on the ListViews?

        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);
like image 32
Oblivion2000 Avatar answered Oct 25 '22 04:10

Oblivion2000