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.

WordWrap

This is a property of the Label Control, which should allow it to dynamically resize vertically. Handled carefully, it will do that. Beginning in VFP 7, it’s also a property of grid headers, allowing their captions to contain multiple lines of text. Given that we’ve been asking for this since VFP 3, it’s about time.

Usage

oObject.WordWrap = lWordWrap
lWordWrap = oObject.WordWrap

This property behaves fairly differently for labels than for headers, so let’s look at them separately. For labels, in theory and from reading the Help file, you might think that WordWrap would turn on and off the capability to wrap a label around, so as it changes it does not take up any more space horizontally, but rather rubber-band-stretches vertically by wrapping words. We initially viewed this as “Vertical AutoSize,” but that’s not exactly how it works.

Setting WordWrap True doesn't automatically wrap your text to a previously set width. You need to either manually set the height of the label, or turn on AutoSize after setting WordWrap .T. before the text will wrap. We expected that WordWrap would cause Height to automatically resize to fit the text, but Microsoft's solution to this makes sense, too.

Be forewarned that the explanation in Help is misleading, if not downright confusing. The most confusing thing there is the implication that the vertical size of the label changes with the text. That’s true only if you set AutoSize to .T.; when it’s .F., the label retains the Height you set. Why is this important? Because all that invisible area can still fire the label’s events. So, a user might click on what looks like empty space and actually fire the label’s Click event. Same thing for MouseEnter and MouseLeave, and all the other events.

For headers, WordWrap is a little simpler, if also stupider. All it does there is allow the text in the header’s Caption property to be wrapped onto multiple lines. It does not resize the header. You need to set the grid’s HeaderHeight for that one—you can do that in code, in the Property Sheet, or by dragging it.

In both cases, you can add CHR(13) to the text to force a new line. If you’re doing this in the Property Sheet, you need to write an expression in the form:

="First part of caption" + CHR(13) + "Second part of caption"

Actually, it wouldn’t look a whole lot different in code. Watch out—if you add CHR(13) to a header’s Caption and fail to set WordWrap to .T., you might see a garbage character where you meant to put a line break.

Example

* Try these from the Command Window.
x=CreateObject("form")
x.Visible = .t.
x.AddObject("MyLabel","Label")
x.mylabel.Visible = .t.
x.mylabel.Caption = "Now I can wrap enough words to occupy " ;
                     + "four lines of text"
* Only as much text as can fit in the default size appears.
x.mylabel.WordWrap = .t.               && Still no more text.
x.mylabel.Height = 4*x.mylabel.Height  && Now it appears!

* An example for the grid header:
USE _Samples + "\data\orders.dbf"
x=CreateObject("form")
x.Visible = .t.
x.AddObject("grdGrid","grid")
x.grdGrid.Visible=.t.
x.grdgrid.column2.header1.Caption = "Multi-line "+CHR(13)+"Demo"
x.grdgrid.column2.header1.WordWrap=.t.
x.grdgrid.HeaderHeight=2*x.grdGrid.HeaderHeight

See Also

AutoSize, Caption, Header, HeaderHeight, Height, Label Control, Width