Winforms – autosize controls in TabPages


I have a TabControl consisting of several TabPages. One of these TabPages contains a TreeView and another contains a DataGridView. I would like these controls (and tabpages) to AutoSize to the maximum size allowed in the TabControl without scrolling.

The DataGridView contains an AutoSize property inherited from Control which garbles the control if enabled. The TreeView does not have this property. I have tried setting the Size equal to TabControl.Size but that does not account for bordersize and the Tabbar height.

treeView.Size = new Size(tabControl.Size.Width - SystemInformation.Border3DSize.Width * 2, tabControl.Size.Height - SystemInformation.Border3DSize.Height * 2);

My question is: how can I determine the height of the Tab buttons or how can I automatically fill TabPages to their maximum size with a single control?

Best Solution

The Property you search is called Dock.

You probably want to set it to DockStyle.Fill on your TreeView and DataGridView.

An alternative would be to set the Anchor Property to AnchorStyle.Top, AnchorStyle.Left, AnchorStyle.Right and AnchorStyle.Bottom (concatenate with binary OR the pipe sign '|'). For that you would need to set the size of the Children to ClientSize

Related Question