我已经解决了~事先要把ShowHint赋值为真!
在ListBox1Click事件中写代码!如下:procedure TForm1.ListBox1Click(Sender: TObject);
var
i: Integer;//i变量用来控制循环的
begin
for i:=0 to ListBox1.Items.Count-1 do //这里因为Listbox的index值从0开始,然后读取listbox总有多少条记录,因为索引从0开始,所以循环要减一
begin
if ListBox1.ItemIndex = i then
begin
ListBox1.Hint := ListBox1.Items[i];
end;
end;
end;
在ListBox1Click事件中写代码!如下:procedure TForm1.ListBox1Click(Sender: TObject);
var
i: Integer;//i变量用来控制循环的
begin
for i:=0 to ListBox1.Items.Count-1 do //这里因为Listbox的index值从0开始,然后读取listbox总有多少条记录,因为索引从0开始,所以循环要减一
begin
if ListBox1.ItemIndex = i then
begin
ListBox1.Hint := ListBox1.Items[i];
end;
end;
end;
TForm1 = class(TForm)
ListBox1: TListBox;
procedure ListBox1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
procedure ListBox1Click(Sender: TObject);
private
{ Private declarations }
i:integer;
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.DFM}procedure TForm1.ListBox1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
begin
ListBox1.hint:=ListBox1.Items[i];end;procedure TForm1.ListBox1Click(Sender: TObject);
begin
i:= ListBox1.ItemIndex;
end;
procedure TForm1.ListBox1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
var
sPoint : TPoint;
sIndex : Integer;
begin
sPoint.x := X;
sPoint.y := Y;
sIndex := TListBox(Sender).ItemAtPos(sPoint,True);
if sIndex <> -1 then
begin
TListBox(Sender).Hint := TListBox(Sender).Items.Strings[sIndex];
GetCursorPos(sPoint);
Application.ActivateHint(sPoint);
end;
end;
if (sIndex<>-1) and (listbox1.Canvas.TextWidth(listbox1.Items.Strings[sIndex])>listbox1.Width) then
begin
listbox1.Hint:=listbox1.Items.Strings[sIndex];
GetCursorPos(sPoint);
listbox1.ShowHint:=true;
Application.ActivateHint(sPoint);
end
else
listbox1.ShowHint:=false;