Author: Tomas Rutkauskas
I would like to pop up a TComboBox at the caret position on a TMemo when the key
that is pressed is a full stop (.). Has anyone got any code for this?
Answer:
1 unit CBoxInMemo;
2 3 interface4 5 uses6 Windows, Classes, Controls, Graphics, Forms, StdCtrls;
7 8 type9 TFrmCboxInMemo = class(TForm)
10 Button1: TButton;
11 Memo1: TMemo;
12 Label1: TLabel;
13 ComboBox1: TComboBox;
14 procedure Button1Click(Sender: TObject);
15 procedure ComboBox1Exit(Sender: TObject);
16 procedure ComboBox1Click(Sender: TObject);
17 private18 { Private declarations }19 public20 { Public declarations }21 end;
22 23 var24 FrmCboxInMemo: TFrmCboxInMemo;
25 26 implementation27 28 {$R *.DFM}29 30 procedure TFrmCboxInMemo.Button1Click(Sender: TObject);
31 var32 clientPos: TPoint;
33 lineHeight: Integer;
34 tmpFont: TFont;
35 begin36 GetCaretPos(clientPos);
37 {Use the following calculation of line height only if you want your combobox38 to appear below the char position you are referencing.}39 tmpFont := Canvas.Font;
40 Canvas.Font := Memo1.Font;
41 lineHeight := Canvas.TextHeight('Xy');
42 Canvas.Font := tmpFont;
43 with ComboBox1 do44 begin45 {Adjustment of Top by lineHeight only necessary if combobox is to appear below 46 line.}47 Top := clientPos.Y + Memo1.Top + lineHeight;
48 Left := clientPos.X + Memo1.Left;
49 Visible := true;
50 SetFocus;
51 end;
52 end;
53 54 procedure TFrmCboxInMemo.ComboBox1Exit(Sender: TObject);
55 begin56 ComboBox1.Visible := false;
57 end;
58 59 procedure TFrmCboxInMemo.ComboBox1Click(Sender: TObject);
60 begin61 ComboBox1.Visible := false;
62 end;
63 64 end.