Logo

Hacker’s Guide to Visual FoxPro
An irreverent look at how Visual FoxPro really works. Tells you the inside scoop on every command, function, property, event and method of Visual FoxPro.

Alignment

This is a property of many controls. Most of the time, it determines where the Caption or Value of the control appears. Whether Alignment applies to Caption or Value depends on the control. For controls that let you type into them (text box, edit box, combo box, spinner), Alignment is about Values. For other controls (check box, option button, header, label), Alignment is about Captions.

For grid columns, Alignment determines the position of the data shown in the column when the contained control doesn’t have focus, and provides a default alignment for the contained control. In this case, it offers a lot more choices than for the other controls.

Usage

oObject.Alignment = nAlignment
nAlignment = oObject.Alignment

Parameter

Value

Meaning

nAlignment

0

Left—this is the default for most controls. For columns, this value means "middle left"—flush left and vertically centered.

1

Right—this is the default for spinners and for a number of controls in the Middle Eastern version of Windows (in other words, for languages written right-to-left). For columns, this is "middle right"—flush right and vertically centered.

2

Center—doesn't apply to some controls. For columns, this is "middle center."

3

Automatic—applies only to text boxes and columns and is their default. It lets them adjust automatically based on whether the Value is character (left-justified) or numeric (right-justified).

4

Top Left—applies to columns and headers only.

5

Top Right—applies to columns and headers only.

6

Top Center—applies to columns and headers only.

7

Bottom Left—applies to columns and headers only.

8

Bottom Right—applies to columns and headers only.

9

Bottom Center—applies to columns and headers only.

First, be aware that, for those controls where Alignment affects Caption rather than Value (check boxes and the like), the meaning of Alignment is backwards from our instinctive reaction. Alignment here controls the position of the control, not the caption. That is, setting a check box to have left alignment means that the box appears to the left and the caption to the right.

Alignment has a whole collection of weird behaviors. With text boxes and spinners, if you use something other than the default alignment, the text or number bounces around when it receives and loses focus. Frankly, we think this is a bug, though it's in the "annoying" category rather than the "keeps you from getting something done" category.

In VFP 3, using right alignment with option buttons causes a problem with the focus rectangle. When focus lands on a right-aligned check box or option button, the left-hand side of the focus rectangle is missing. This bug was fixed in VFP 5.

Each option button has its own Alignment property. So, some buttons in a group may be left-aligned while the others are right-aligned. Though we can’t imagine why anyone would want to do this, this option is available.

However, changing to right-alignment has another problem. When you right-align option buttons, by default, they’re still lined up within the group based on their left edges. That is, the little circles don’t line up. To solve the problem, you have to select the individual buttons and use the Layout toolbar or the Format MENU TO align the right edges.

Alignment and AutoSize have a strange relationship. For labels, setting AutoSize to .T. means that the value of Alignment is irrelevant. For check boxes and option buttons, you may want AutoSize set to .T. so that the entire caption shows—actually this isn’t particularly related to Alignment, but the problem looks much stranger when using right-alignment than with left.

Alignments other than Left are a mess in combo boxes. When you use Center or Right alignment, the value in the combo disappears entirely in VFP 3. In VFP 5 and later, the value doesn't get aligned properly until you leave the combo. As long as it still has focus, the value is too far to the left.

For grid columns and headers, Alignment has a lot more choices than in the other cases. You can specify not just the horizontal alignment of the item, but its vertical alignment, too. For these controls, Automatic means centered vertically, but with horizontal alignment determined by the data type.

Non-Sparse columns don't pick up their vertical alignment immediately. When you start the form, the data is at the top. As soon as you click into the column, the data moves to the correct vertical position. You have to tab or click into each cell or do some scrolling to get all the data where it should be. Yuck!

Columns containing check boxes don't let you use any of the center alignments to center the check box itself in the column. In addition, check boxes in grids ignore the vertical alignment you specify (unless it's middle).

As far as we can tell, Alignment does work properly for headers and labels. That’s a start.

Example

oForm = CREATE("Form")
oForm.Show()
oForm.AddObject("MyCheck", "CheckBox")
oForm.MyCheck.Visible = .T.
oForm.MyCheck.AutoSize = .T.
oForm.MyCheck.Caption = "Checker"
oForm.MyCheck.Alignment = 1  && Right

See Also

AutoSize, Caption, DynamicAlignment, Value