2007. március 9., péntek

TCheckBox inside a TRichEdit


Problem/Question/Abstract:

With my colleagues, we tried to implement a checkbox object into Rich Edit.
Is that possible anyway?

Answer:

This seems not possible from within the IDE/ Object Inspector. However, if you create the checkbox instance dynamically (at run-time), then it works as expected.
For the following example, create a new form, drop a TRichEdit on it and create the checkbox in the FormCreate() event.

The button event handler code moves the RichEdit control at run-time around to demonstrate that the checkbox really is its child.

unit fMain;

interface

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

type
  TForm1 = class(TForm)
    RichEdit1: TRichEdit;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
begin
  RichEdit1.Left := 100 - RichEdit1.Left;
end;

procedure TForm1.FormCreate(Sender: TObject);
var
  cb: TCheckBox;
begin
  RichEdit1.Left := 20;

  cb := TCheckBox.Create(RichEdit1);
  // do not forget to set the
  cb.Parent := RichEdit1;
  cb.Left := 30;
  cb.Top := 30;
  cb.Caption := 'my checkbox';
end;

end.

Nincsenek megjegyzések:

Megjegyzés küldése