Using Control's Child Index and Dock property for achieving desired layout in a form.

In this article, i will teach you , how to use control's child index property while docking.

If you dock controls in a form or in a container, they will appear in order of their child index. You can dynamically set child index of controls.

Scenario: I have 3 controls in my form. Datagridview, label and button. I want my datagridview to fill my form. I want my label and button to appear at bottom. I want my datagridview to fill all the empty space.

if i add the 3 controls and  write the following code and run it , the form will looks as below

dataGridView1.Dock = DockStyle.Fill;
            label1.Dock = DockStyle.Bottom;
            button1.Dock = DockStyle.Bottom;
dock fill child index
In the above image, button and label are on the top of the datagridview.
 To avoid this, Let us set the child index.

this.Controls.SetChildIndex(dataGridView1, 0);
this.Controls.SetChildIndex(label1, 1);
this.Controls.SetChildIndex(button1, 2);

Now the output looks as below Child index dock fill winforms

What differences can you find when you compare the above 2 images ?

 In the first image , button and label are on the top of the grid or overlapped with the grid while in the second image they are properly aligned to the bottom of the form.
This is because we had given the child index.

Now if we make the label visible false, the datagridview will occupy the empy space as it has dockstyle property to fill as in the below screenshot.

dockstyle fill winforms control

Zooming in Winforms Webbrowser Control in .NET
Filtering Datatable with Select Conditions and Sorting in C#.NET
Hiding a property from displaying in Datagridview in C#.NET
Logic to detect if Application is started from Citrix/Terminal Server/PC in C#
Winforms Application.DoEvents Method
Modify XML file with tagname in
Read Machine IP Address in C#
Check if IIS is running using C#
Implementing IEnumerable in C#
Built in Delegates in .NET Framework
Changing Winform webbrowser control IE version in C#.NET
Set the dropdown width of any combobox based on the data
Difference between Const and ReadOnly
Example for Property Change Notification in C#.NET
Example Program using Delegates and Events in C#
AutoComplete in .NET Winforms TextBox
How to read data from XML String and insert in to table in SQL Server
WebBrowser.navigate Data Submission by Get or Post Methods
Datagridview Paging
Copy Datagridview cell value using ContextMenu
Inserting data in Datatable in C#
Cookie in .NET
Accessing section group inside configuration of web.config
Create File in .NET C#
Change button shape in .net
Dispose vs Finalize in .net