Anchor Property
Defines which borders of the parent container that a visual control is anchored to when resizing the container. Read/write at design time and run time.
Control.Anchor [= nValue]
Return Value
nValue
Specifies a bit value that determines the anchor behavior of the control.The following table describes bit values you can set or add to produce values for nValue.
Position
Bit value
Bit number
Conflicting values
Description
Top Left
0
Anchors control to the top and left borders of the container and does not change the distance between the top and left borders. (Default)
Top Absolute
1
0
16, 512
Anchors control to top border of container and does not change the distance between the top border.
Left Absolute
2
1
32, 256
Anchors control to left border of container and does not change the distance between the left border.
Bottom Absolute
4
2
64, 512
Anchors control to bottom border of container and does not change the distance between the bottom border.
Right Absolute
8
3
128, 256
Anchors control to right border of container and does not change the distance between the right border.
Top Relative
16
4
1, 512
Anchors control to top border of container and maintains relative distance between the top border.
Left Relative
32
5
2, 256
Anchors control to left border of container and maintains relative distance between the left border.
Bottom Relative
64
6
4, 512
Anchors control to bottom border of container and maintains relative distance between the bottom border.
Right Relative
128
7
8, 256
Anchors control to right border of container and maintains relative distance between the right border.
Horizontal Fixed Size
256
8
2, 8, 32, 128
Anchors center of control relative to left and right borders but remains fixed in size.
Vertical Fixed Size
512
9
1, 4, 16, 64
Anchors center of control relative to top and bottom borders but remains fixed in size.
The following table describes examples of common settings for the Anchor property.
Control
nValue
Description
TextBox or EditBox
10
Resizes control horizontally as you resize it.
CommandButton
12
Displays a set of two controls, for example, OK and Cancel, in the lower right corner.
CommandButton
128 and 32
Centers two left and right command buttons.
CommandButton
260
Centers control on the bottom of a form.
Remarks
Applies To: CheckBox Control | ComboBox Control | CommandButton Control | CommandGroup Control | Container Object | Control Object (Visual FoxPro) | EditBox Control | Grid Control | Image Control (Visual FoxPro) | Label Control (Visual FoxPro) | Line Control | ListBox Control | OLE Bound Control | OLE Container Control | OptionButton Control | OptionGroup Control | PageFrame Control | Shape Control | Spinner Control | TextBox Control (Visual FoxPro)
Anchoring is based on the coordinates of the control at the time when the control's Anchor property is originally set, typically, when the control is instantiated. It is not based on the control's current position. Therefore, even after you move a control, anchoring applies to the control's original position. This behavior explains the difference between an Anchor value of 0 and 3.
For example, suppose you have a CommandButton control on a form and the Anchor property for the control is initially set to 3. By inserting the following code in the control's Click event and clicking the button, you can move the command button:
This.Left = This.Left + 10
However, when you resize the form, the control returns to its original position.
Tip
If you want to base anchoring on the control's current position after it is has been moved, reset the control's Anchor property to 0 and then back to 3. For example, using the following code in the command button's Click event moves the command button when you click it but retains the control's current position when the form is resized:
This.Left = This.Left + 10
This.Anchor = 0
This.Anchor = 3
Settings for the Anchor property are obeyed for applicable controls in the following containers:
Settings for the Anchor property are disregarded for controls in the ToolBar and Column objects.
You can resize the control if anchor values are set for both opposing edges, for example, left and right edges or top and bottom edges. You can only move the control if you set a single edge.
If you do not set bits for a particular axis, the control's position does not change for that axis when the container resizes. For example, the bit numbers 1, 3, 5, 7, and 10 affect the top and bottom dimension.
You cannot set bit values that conflict with each other. These bit values include the following:
Absolute and Relative bits for the same border, for example, an nValue of 17.
Fixed Size and Border bits for the same axis borders, for example, an nValue of 258 or 513.
For a description of the conflicting values, see the table listing the values for nValue.