0

I am trying to create an TCustomRichEdit with some speedbuttons above it. How can I accomplish this behaviour? The height of the component needs to be the buttons + the richedit.

[B][I][U]
+-------+
|A1REdit|
|       |
+-------+

Currently I have the following code:

private
  FBoldButton: TSpeedButton;

constructor TA1RichEdit.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);

  FBoldButton := TSpeedButton.Create(Self);
  FBoldButton.Parent := Self;
end;

destructor TA1RichEdit.Destroy;
begin
  FreeAndNil(FBoldButton);

  inherited;
end;

This paints the speedbutton on the richedit (because the richedit is the parent), I need the button to be above the richedit. The richedit doesnt paint itself like it should be after this piece of code.

A1rPun
  • 16,287
  • 7
  • 57
  • 90
  • 2
    Make a panel with richedit and buttons as childs – whosrdaddy Jun 12 '12 at 14:57
  • 3
    `TToolbar` sounds like the right solution to me. But you can't use the richedit as the parent of the toolbar (or buttons). You need another container, e.g. panel, plus toolbar (alTop) and then rich edit (alClient). – David Heffernan Jun 12 '12 at 15:06
  • 1
    related: http://stackoverflow.com/questions/10902749/is-it-wise-to-create-composite-controls – Warren P Jun 13 '12 at 02:00
  • Using a panel as a container was indeed the solution I was looking for! The Toolbar has all the functionality I need so I'm going to use that approach :) – A1rPun Jun 13 '12 at 09:45

2 Answers2

5

Use a TToolbar.

Also put a TImageList and a TActionList on your form.

Assign the ImageList to your ActionList and ToolBar.

Then you can add some so called standard actions to your ActionList (the drop down menu on the "New Action" Button).

In the Format category you will find TRichEditBold, TRichEditItalic and TRichEditUnderline. Select them and click OK.

Then you add 3 tool buttons to your toolbar and assign your actions to them.

Everything like the pressed state when something is selected that has this state and automatically setting bold, italic or underlined will be handled without writing any code.

Stefan Glienke
  • 20,860
  • 2
  • 48
  • 102
  • I think (based on the class name in the code provided) that the question is about creating a component with a toolbar above a richedit). Not downvoting, but I think you've misread the question. :-) – Ken White Jun 13 '12 at 02:33
  • 1
    I didn't misread but my suggestion is to do it like this instead of adding the buttons to the richedit itself because actually there is no benefit imo. Using a frame as Warren pointed out does the job pretty well and behaves like a component. – Stefan Glienke Jun 13 '12 at 06:14
  • This is indeed not the answer to my Question but the best solution for my problem. I'm going to do it like this :) – A1rPun Jun 13 '12 at 09:41
  • 1
    After you get a few miles down this road, and you lose some hair, and have some sleepless nights fighting TRichEdit, you might want to ditch it all and go look at `TRichView`. It actually works, all the time, unlike TRichEdit. http://www.trichview.com/ – Warren P Jun 14 '12 at 13:48
2

Consider making a Frame. A frame is the usual way to make "composite controls", and doesn't require any coding, in your case it can be done with just the form designer and property inspector.

Click File -> New -> Frame (you might have to click File -> New -> other and then find frame if it isn't already in your new-items-menu).

Now add the buttons, and the Rich Edit. Set the Anchors on the Rich Edit to Top + Left + Right + Bottom (all four On).

Now you can place this "frame" anywhere in your application and it will act like a single control.

What I don't like about Frames is that you can't stop someone who instantiates a frame from editing its layout, by default they are allowed to move the controls around inside the frame and add more controls, and this is in fact, kind of a pain about Frames.

A second approach you could look at is a "composite control". Inherit from TPanel, and add the RIch Edit and your buttons, writing it all in code. The height and layout code is yours to write however you like. The difference between that, and what you've suggested, is you're trying to make a Rich Edit contain other controls, which as David has clearly said, is not possible.

Warren P
  • 65,725
  • 40
  • 181
  • 316
  • I think the question is about creating a component (not a frame), based on the class name in the code sample. A frame is great, but it's not a replacement for a component in every case. – Ken White Jun 13 '12 at 02:34
  • Thanks Warren for another solution ;) – A1rPun Jun 13 '12 at 09:48
  • Agreed. But perhaps the user actually wants component-like reuse, which is what Frames were made to do. Visually creating "composite components" made of several individual visual components (controls). – Warren P Jun 13 '12 at 13:02