DelphiFAQ Home Search:

Getting a Line Break on a Button's caption

 

comments1 comments. Current rating: 5 stars (1 votes). Leave comments and/ or rate it.

Question:

How do I get linebreak on a button's caption?

Answer 1:

You could use a SpeedButton or a BitButton instead of TButton. The disadvantage is that they are not native Windows components, thus overhead, different inheritance, other (window) messages e.g. the TSpeedButton cannot receive the focus.

Better solution -> Answer 2:

Answer 2:

Make a TButton descendent that has the BS_MULTILINE button style. Below is such a class which allows you to use the pipe character ('|') as a substitute for #13 in the designer when setting the button's caption.

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 { register }
  RegisterComponents('Samples', [TMultilineButton]);
end; { register }


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


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


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


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


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

end.
You don't like the formatting? Check out SourceCoder then!
Content-type: text/html

Comments:

2013-09-09, 02:53:50
anonymous from Hoogland, Netherlands  
rating

 

 

NEW: Optional: Register   Login
Email address (not necessary):

Rate as
Hide my email when showing my comment.
Please notify me once a day about new comments on this topic.
Please provide a valid email address if you select this option, or post under a registered account.
 

Show city and country
Show country only
Hide my location
You can mark text as 'quoted' by putting [quote] .. [/quote] around it.
Please type in the code:

Please do not post inappropriate pictures. Inappropriate pictures include pictures of minors and nudity.
The owner of this web site reserves the right to delete such material.

photo Add a picture: