现在的位置: 首页 > 综合 > 正文

输入“\”或“.”的时候弹出选择对话框,让你选择?

2013年05月07日 ⁄ 综合 ⁄ 共 1441字 ⁄ 字号 评论关闭

unit Unit1;
 
interface
 
uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;
 
type
  TForm1 = class(TForm)
    Memo1: TMemo;
    ListBox1: TListBox;
    procedure Memo1KeyPress(Sender: TObject; var Key: Char);
    procedure ListBox1DblClick(Sender: TObject);
    procedure Memo1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
 
var
  Form1: TForm1;
 
implementation
 
{$R *.DFM}
 
procedure TForm1.Memo1KeyPress(Sender: TObject; var Key: Char);
const
  cInfo = ['/', '.'];
var
  cPos: TPoint;
  i: integer;
  cText: String;
begin
  GetCaretPos(cPos);
  if Key in cInfo then
  begin
    for i := 0 to ListBox1.Items.Count-1 do
    begin
      cText := ListBox1.Items[i];
      if cText[1] = Key then
      begin
        ListBox1.ItemIndex := i;
        Break;
      end;
    end;
    ListBox1.Visible := True;
    ListBox1.Left := TMemo(Sender).Left+cPos.x;
    ListBox1.Top := TMemo(Sender).Top+cPos.y+15;
  end else
  begin
    ListBox1.Visible := False;
  end;
end;
 
procedure TForm1.ListBox1DblClick(Sender: TObject);
begin
  Memo1.Text := Copy(Memo1.Text, 0, Length(Memo1.Text)-1);
  Memo1.Text := Memo1.Text+ListBox1.Items[ListBox1.ItemIndex];
  ListBox1.Visible := False;
  Memo1.SetFocus;
  Memo1.SelStart := Length(Memo1.Text);
end;
 
procedure TForm1.Memo1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  ListBox1.Visible := False;
end;
 
procedure TForm1.FormCreate(Sender: TObject);
begin
  ListBox1.Visible := False;
end;
 
end.

抱歉!评论已关闭.