Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Winforms Dynamic size

I have ma de a winform, that contains a tabControl (3 tabs, maybe more later).

In 2 of my tabs, I got a listBoxView.

The problem is, when I click on fullSize button, the tabControl doesn't change his size. That makes an awful window.

How can I define dynamic size of my tabControl, based on the winforms border size, and a dynamic size of my listBoxView based on tabControl size?

TabControl must adapt to form size, then pages in tabControl must adapt to tabControl size, and then, listBox in the pages must adapt to its pages size.

Here is the form :

    // 
    // Form1
    // 
    this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
    this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
    this.ClientSize = new System.Drawing.Size(1313, 614);
    this.Controls.Add(this.tabControl1);
    this.tabControl1.ResumeLayout(false);
    this.tabPage1.ResumeLayout(false);
    this.tabPage1.PerformLayout();
    ((System.ComponentModel.ISupportInitialize)(this.dgCSV)).EndInit();
    this.tabPage2.ResumeLayout(false);
    this.tabPage2.PerformLayout();
    this.ResumeLayout(false);

And the tabControl with one of his pages :

 // 
        // tabControl1
        // 
        this.tabControl1.Controls.Add(this.tabPage1);
        this.tabControl1.Controls.Add(this.tabPage2);
        this.tabControl1.Controls.Add(this.tabPage3);
        this.tabControl1.Location = new System.Drawing.Point(13, 13);
        this.tabControl1.Name = "tabControl1";
        this.tabControl1.SelectedIndex = 0;
        this.tabControl1.Size = new System.Drawing.Size(1288, 589);
        this.tabControl1.TabIndex = 0;
    // 
    // tabPage2
    // 
    this.tabPage2.Controls.Add(this.listBoxFiles);
    this.tabPage2.Controls.Add(this.richTextBox1);
    this.tabPage2.Controls.Add(this.buttonBottom);
    this.tabPage2.Controls.Add(this.buttonFront);
    this.tabPage2.Controls.Add(this.buttonDown);
    this.tabPage2.Controls.Add(this.buttonUp);
    this.tabPage2.Controls.Add(this.label2);
    this.tabPage2.Location = new System.Drawing.Point(4, 25);
    this.tabPage2.Name = "tabPage2";
    this.tabPage2.Padding = new System.Windows.Forms.Padding(3);
    this.tabPage2.Size = new System.Drawing.Size(1280, 560);
    this.tabPage2.TabIndex = 1;
    this.tabPage2.UseVisualStyleBackColor = true;

I tried with Parent.width, ClientRectangle, ClientSize.

I'm lost with all this properties and no one is successfull...

After trippino answer :

Indeed, a dock on the tabControl will resize him, but I can't do it on a listBoxView, because one of the element will just take all the page.

And Anchor do not resize elements, it will juste reorganise them to fit in the page.

Still not resizing like this :

    // 
    // tabControl1
    // 
    this.tabControl1.Controls.Add(this.tabPage1);
    this.tabControl1.Controls.Add(this.tabPage2);
    this.tabControl1.Controls.Add(this.tabPage3);
    this.tabControl1.Dock = System.Windows.Forms.DockStyle.Fill;
    this.tabControl1.Location = new System.Drawing.Point(0, 0);
    this.tabControl1.Name = "tabControl1";
    this.tabControl1.SelectedIndex = 0;
    this.tabControl1.Size = new System.Drawing.Size(1313, 614);
    this.tabControl1.TabIndex = 0; 

    // 
    // listBoxFiles
    // 
    this.listBoxFiles.Anchor = System.Windows.Forms.AnchorStyles.Right;
    this.listBoxFiles.Anchor = System.Windows.Forms.AnchorStyles.Top;
    this.listBoxFiles.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
    this.listBoxFiles.Anchor = System.Windows.Forms.AnchorStyles.Left;
    this.listBoxFiles.FormattingEnabled = true;
    this.listBoxFiles.ItemHeight = 16;
    this.listBoxFiles.Location = new System.Drawing.Point(185, 43);
    this.listBoxFiles.Name = "listBoxFiles";
    this.listBoxFiles.SelectionMode = System.Windows.Forms.SelectionMode.MultiExtended;
    this.listBoxFiles.Size = new System.Drawing.Size(1040, 244);
    this.listBoxFiles.TabIndex = 42;



private void tabPage2_SizeChanged(object sender, EventArgs e)
{
    this.buttonAucun.Location = new System.Drawing.Point(this.buttonAucun.Location.X, this.listBoxFiles.Location.Y + this.listBoxFiles.Height + 10);
    this.progressBar1.Location = new System.Drawing.Point(this.progressBar1.Location.X, this.buttonAucun.Location.Y + this.buttonAucun.Height + 10);
    this.richTextBox1.Location = new System.Drawing.Point(this.richTextBox1.Location.X, this.progressBar1.Location.Y + this.progressBar1.Height + 10);
    this.buttonEnregistrer.Location = new System.Drawing.Point(this.buttonEnregistrer.Location.X, this.richTextBox1.Location.Y + this.richTextBox1.Height + 10);

    }

Since, buttonEnregistrer is on the bottom of my tabPage, I also tried :

    this.buttonEnregistrer.Location = new System.Drawing.Point(this.buttonEnregistrer.Location.X, this.tabPage2.Height -50);

But his will not retake it's original location.

Thank you.

like image 474
provençal le breton Avatar asked Oct 03 '22 09:10

provençal le breton


1 Answers

Just use the Dock property of the tabControl setting it to Fill. It should solve your issue.

MSDN Dock Property reference

EDIT AFTER DISCUSSION: to anchor 4 sides you have to use :

this.listBoxFiles.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)));
like image 78
Tobia Zambon Avatar answered Oct 13 '22 10:10

Tobia Zambon