delphi中Memo1下拉ListBox2选择完成文本填充
拖一个Memo1下拉ListBox2到界面上,假设ListBox2,里已加载列名
最终效果:
Memo1里输入.点号,带出ListBox2,选择好列名,回车,将点号和列名输入到Memo1原位置
procedure TForm8.Memo1KeyPress(Sender: TObject; var Key: Char);
var a:ansiString;
begin
if (Key='.') then begin
a:= leftStr( Memo1.Lines[Memo1.CaretPos.Y ] , Memo1.CaretPos.x) ;
ListBox2.Left:=length(a) * memo1.Font.Size+9;
ListBox2.Top:=(memo1.CaretPos.Y-Memo1.Perform(EM_GETFIRSTVISIBLELINE,0,0)+2)*abs(memo1.Font.Height)+22;
ListBox2.BringTofront;
ListBox2.Visible:=True;
if ListBox2.CanFocus then ListBox2.SetFocus;
Key:=#0;
end
else ListBox2.Visible:= false;
end;
procedure TForm8.ListBox2KeyPress(Sender: TObject; var Key: Char);
begin
if key=#27 then begin listbox2.Visible:=false; exit end; // Esc 隐藏ListBox2
if key =#13 then begin
Memo1.SelText := '.'+listBox2.Items[listBox2.ItemIndex] ; //将 文本插入到Memo1
listbox2.Visible:=false;
memo1.SetFocus;
end;
end;
鼠标双击,将 点选项 输入到Memo1
procedure TForm8.ListBox2DblClick(Sender: TObject);
begin
if ListBox2.ItemIndex >= 0 then
begin
Memo1.SelText := '.'+listBox2.Items[listBox2.ItemIndex] ; //将 文本插入到Memo1
listbox2.Visible:=false;
memo1.SetFocus;
end;
end;