Add the code given below in the form constructor.
Listing 4
public frmDynamicallyLoadingCtrls()
{
InitializeComponent();
string[] str = { "Field 1", "Field 2", "Field 3", "Maximum Length Field 4",
"Field 5", "Field 6" };
CreateControls(str);
}
In the above listing we initialize the fields to be
displayed on the form. Six fields are defined in the string array and it is
passed to CreateControls method to dynamically load the controls.
Now run the form upon and the following screen will be
displayed.
Figure 1
Add the field names as below in the form constructor and run
the form again.
Listing 5
public frmDynamicallyLoadingCtrls()
{
InitializeComponent();
string[] str = { "Field 1", "Field 2", "Field 3", "Maximum Length Field 4",
"Field 5", "Field 6", "Very Very long Maximum Length Field 7", "Field 8" };
CreateControls(str);
}
Figure 2
If you notice in both screens, Field name lengths are
resized, positioned and aligned based on maximum length of label field name
controls. Others controls are also aligned and positioned accordingly.
Similarly, try adding or deleting field names to the string array in Listing 4.