Jump to content
aehimself

Changing the WordWrap property of a TMemo runtime

Recommended Posts

Hello,

 

I would like to have a TMemo in my application, where the user could manually toggle the WordWrap property. When changing this property though runtime, the change has no effect on the display whatsoever.

I did my homework, and I see only one reference to FWordWrap in the TCustomMemo implementation, which is this block:

procedure TCustomMemo.CreateParams(var Params: TCreateParams);
const
  ScrollBar: array[System.UITypes.TScrollStyle] of DWORD = (0, WS_HSCROLL, WS_VSCROLL,
    WS_HSCROLL or WS_VSCROLL);
  WordWraps: array[Boolean] of DWORD = (0, ES_AUTOHSCROLL);
begin
  inherited CreateParams(Params);
  with Params do
    Style := Style and not WordWraps[FWordWrap] or ES_MULTILINE or ScrollBar[FScrollBars];
end;

The setter looks like this:

procedure TCustomMemo.SetWordWrap(Value: Boolean);
begin
  if Value <> FWordWrap then
  begin
    FWordWrap := Value;
    RecreateWnd;
  end;
end;

Placing a breakpoint in CreateParams confirms that RecreateWnd triggers the call of CreateParams, where FWordWrap already has the most recently set value, however it does not change the behavior.

 

Google only lead me to one question on the FPC forums, but with no viable solution. Is there a way to make this work without using a 3rd party component?

Share this post


Link to post

Have you tried saving the value of the Text property before changing the WordWrap property, and then re-assign the Text property afterwards?

Share this post


Link to post

@Remy Lebeau No, not yet, was a good idea. Unfortunately though it makes no difference:

Var
 s: String;
begin
 s := Memo1.Lines.Text;
 Memo1.Clear;
 Memo1.WordWrap := Not Memo1.WordWrap;
 Memo1.Lines.Text := s;
end

 

Share this post


Link to post

No, RichEdit works just fine. And I accidentally found the solution... it's the scrollbar.

Begin
 TextEditor.WordWrap := Not TextEditor.WordWrap;
End;

doesn't do anything.

Begin
 TextEditor.WordWrap := Not TextEditor.WordWrap;
 If TextEditor.WordWrap Then TextEditor.ScrollBars := ssVertical
   Else TextEditor.ScrollBars := ssBoth;
End;

works like a charm.

Share this post


Link to post

Seems legit. If you have a horizontal scroll bar there is no need for wrapping anything.

Share this post


Link to post
Just now, Uwe Raabe said:

Seems legit. If you have a horizontal scroll bar there is no need for wrapping anything.

I can see the logic in there but this is why scrollbars can be disabled. I expected that the horizontal scrollbar will become disabled and word wrapping starts to happen.

 

Well, anyway. At least it works now.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×