«« Site Rescued »»

While the site is still going to move from its current host, a new site will now take its place. More Info.

The new site may have less content, but the core will now remain. And it will now play nicely with phones! Keep an eye on the DelphiDabbler Blog for news.

» Help

How to implement a multi-line caption on a TButton

   
Question
How do I make a button have a two line caption? I think there is a character or sequence there to embed a linefeed in the caption property.

It is not as simple as adding a #13#10 sequence as a line break in the caption. You also need to add the BS_MULTILINE style. The following sample component will accept a pipe character ("|"") in the caption as proxy for a linebreak. This allows you to specify the break in the designer, which does not accept Return as part of the caption string.

If you want to be able to include #13#10 in a caption using the designer try the Extended String Property Editor IDE extension.
unit MLButton;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TMultilineButton = class(TButton)
  private
    FMultiline: Boolean;
    function GetCaption: String;
    procedure SetCaption(const Value: String);
    procedure SetMultiline(const Value: Boolean);
  public
    procedure CreateParams(var params: TCreateParams); override;
    constructor Create(aOwner: TComponent); override;
  published
    property Multiline: Boolean
    	read FMultiline write SetMultiline default True;
    property Caption: String read GetCaption write SetCaption;
  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('PBGoodies', [TMultilineButton]);
end;

constructor TMultilineButton.Create(aOwner: TComponent);
begin
  inherited;
  FMultiline := True;
end;

procedure TMultilineButton.CreateParams(var params: TCreateParams);
begin
  inherited;
  if FMultiline then
    params.Style := params.Style or BS_MULTILINE;
end;

function TMultilineButton.GetCaption: String;
begin
  Result := Stringreplace( inherited Caption, #13, '|', [rfReplaceAll] );
end;

procedure TMultilineButton.SetCaption(const Value: String);
begin
  if value <> Caption then
  begin
    inherited Caption := Stringreplace( value, '|', #13, [rfReplaceAll] );
    Invalidate;
  end;
end;

procedure TMultilineButton.SetMultiline(const Value: Boolean);
begin
  if FMultiline <> Value then
  begin
    FMultiline := Value;
    RecreateWnd;
  end;
end;

end.
Original resource: The Delphi Pool
Author: Peter Below
Added: 2009-08-24
Last updated: 2009-08-24

« Return to contents »