3

I use TMemo to be able to display multiple lines.

I want to change selected text attribute in TMemo into bold using the shortcut Ctrl+B.

For example, User enters "Hello, how are you?" in the Tmemo, I want when when user selects "How" and press Ctrl+B then only "How" should be appeared in Bold in that TMemo.

I use Delphi 7.

Please advice to get solution. thanks for help.

menjaraz
  • 7,551
  • 4
  • 41
  • 81
Nalu
  • 1,107
  • 4
  • 20
  • 43
  • 4
    TMemo doesn't support that out of the box and it would require a lot of work to get it to do so. You may be better off switching to a TRichEdit and learn how to work with its formatting. – Marjan Venema Dec 26 '11 at 10:29
  • Can't get `TMemo` to do it at all. Trivial with `TRichEdit`. – David Heffernan Dec 26 '11 at 10:54
  • Any hint to make it possible using TRichEdit? thanks – Nalu Dec 26 '11 at 11:09
  • I am able to set font as Bold using RichEdit. Can someone help me to set the hotkey Ctrl+B to call xyz procedure. – Nalu Dec 26 '11 at 12:40
  • @DavidHeffernan: You are probably right. I kept the possibility open because the TMS component suite has highlighting available in its memo components, and I didn't know their ancestry off the top of my head. Checked it. They are descending straight from TCustomControl... – Marjan Venema Dec 26 '11 at 12:53
  • @naren About having a Hotkey try this on `OnKeyDown` event `if (((GetKeyState(VK_control) AND 128)=128) AND (key=Ord('B'))) then begin //ur procedure end;` – Shirish11 Dec 26 '11 at 12:58

2 Answers2

3

You can't format text in a memo control. You need a rich edit control, TRichEdit.

In order to make the current selection bold you do this:

RichEdit.SelAttributes.Style := RichEdit.SelAttributes.Style + [fsBold];

The preferred way to invoke code in response to a shortcut like CTRL+A is to use actions. Add a TActionList to the form and add an action to that action list. Set the action's OnExecute event handler to point at code that performs the bolding of the selected text. Set the Shortcut property to Ctrl+A. Use actions so that you can centralise the control of user events. Typically there may also be a tool button, a menu item and a context menu item that performed the same action and this is where actions come into their own.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
2

Here's part of a program which I wrote which uses a RichEdit; part of the line is displayed in black, part in blue and possibly part in bold red. 'Text' is a field of the RichEdit.

procedure TWhatever.InsertText (const atext, btext, ctext: string);
begin
 with RichEdit1 do
  begin
   selstart:= length (text);
   sellength:= 0;
   SelAttributes.Color:= clBlack; 
   seltext:= '[' + atext + '] ';

   selstart:= length (text);
   sellength:= 0;
   SelAttributes.Color:= clBlue;
   seltext:= btext + ' ';

   if ctext <> '' then
    begin   // trap non-existent answers
     selstart:= length (text);
     sellength:= 0;
     SelAttributes.Color:= clRed;
     SelAttributes.Style:= [fsBold];
     seltext:= ctext + ' ';
     SelAttributes.Style:= [];
    end;
   lines.add ('');  // new line
  end;
end;
No'am Newman
  • 6,395
  • 5
  • 38
  • 50
  • I am able to set font as Bold using RichEdit. Can someone help me to set the hotkey Ctrl+B to call xyz procedure. – Nalu Dec 26 '11 at 12:40
  • Why is there no bold in this answer? – David Heffernan Dec 26 '11 at 14:18
  • @David: Because I didn't notice 'bold' in the question! I'll edit my answer. – No'am Newman Dec 27 '11 at 05:36
  • To get Ctrl+B to work as a shortcut I would reccomend using the TActionManager or TActionList for your events (actions). Put shortcut property to Ctrl+B then when you press that on your keyboard your action/event will trigger. –  Mar 11 '12 at 12:51