Senin, 29 Juli 2013

Specify relative positions of controls in window form C#

When a window form is open, working normally, all the controls are set as the programmer set them with their parent form, it means that form is in normal state. Resize the form and check that the controls still remains of the same size and additional space is blank on the form.

Anchor property of the form is used to put the controls to its relative position on the form, means a control is automatically take its relative position as its parent control is resized. Each control has its four edges i.e. left, top, right and bottom. By default a control is added to the form with its Anchor property to top and left. When we resize a form then the control is still in top and left corner of the form and size of the control is fixed.

For example drag and drop a listview in the form and resize the form, the listview will not change its points of all the four edge.

Anchor property of the control in windows form C#

In the properties window change the anchor property of the listview to “Top, Bottom, Left, Right”, run the form and check the difference.

Anchor property of controls in windows forms C#

This is how we have specify the relative position of the control from designer part. We can set this anchor property through code behind file

listView1.Anchor = AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right | AnchorStyles.Bottom;

Anchor property and dock property are mutually exclusive, means one can be set at a time. The last property change will be effective in both of these two.

Tidak ada komentar:

Posting Komentar