唉,问个listbox的小问题

qyq1973 2003-08-21 04:41:59
如何鼠标在listbox上移动的时候,会自动选择项目;
当项目的宽度大于listbox宽度的时候,能让它自动分行吗?
...全文
20 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
IORILI 2003-08-22
  • 打赏
  • 举报
回复
上面程序实现了:
当项目的宽度大于listbox宽度的时候,能让它自动分行
IORILI 2003-08-22
  • 打赏
  • 举报
回复
更正:
procedure TForm1.FormCreate(Sender: TObject);
var
i,j,count,lent, MaxWidth: integer;
str,cstr:string;
begin
MaxWidth := round(listbox1.ClientWidth/13) ;
for i := 0 to ListBox1.Items.Count - 1 do
begin
showmessage(ListBox1.Items.Strings[i]);
// maxwidth:=ListBox1.Canvas.TextWidth(ListBox1.Items.Strings[i]);
if MaxWidth < ListBox1.Canvas.TextWidth(ListBox1.Items.Strings[i]) then
str:=ListBox1.Items.Strings[i];
// lent:=length(str);
lent:=round(ListBox1.Canvas.TextWidth(ListBox1.Items.Strings[i])/13);
count:=round(lent/maxwidth);
ListBox1.Items.Strings[i]:=Copy(str,1,MaxWidth);
for j:=1 to count do
begin
cstr:=Copy(str,maxwidth+1,maxwidth*j+1);
ListBox1.Items.Strings[i+j]:=Copy(str,maxwidth+1,maxwidth);
maxwidth:=maxwidth*j+1;
end;
end;
end;
IORILI 2003-08-22
  • 打赏
  • 举报
回复
procedure TForm1.FormCreate(Sender: TObject);
var
i,count,lent, MaxWidth: integer;
str:string;
begin
MaxWidth := listbox1.ClientWidth ;
for i := 0 to ListBox1.Items.Count - 1 do
if MaxWidth < ListBox1.Canvas.TextWidth(ListBox1.Items.Strings[i]) then
str:=ListBox1.Items.Strings[i];
lent:=length(str);
count:=(lent div maxwidth);
ListBox1.Items.Strings[i]:=Copy(str,1,MaxWidth);
ListBox1.Items.Strings[i+1]:=Copy(str,maxwidth+1,lent);
end;
wooden954 2003-08-22
  • 打赏
  • 举报
回复
对于第二个问题,由于ListBox没有像WordWrap之类的属性,所以不能直接实现你要求的功能,如果一定要这么做,只能用别的控件或自己写更多的代码了,如果自己写,实现方法我也不知道怎么办
zhoutian618 2003-08-22
  • 打赏
  • 举报
回复
自己写OnMouseMove事件实现选择某个项目;
自己写OnDrawItem和OnMeasureItem事件,且Style设置为lbOwnerDrawVariable
wooden954 2003-08-22
  • 打赏
  • 举报
回复
对于第一个问题,在ListBox的OnmouseMove事件中可以写代码来让其选择,可以用ItemAtPos函数来取得鼠标所在处的项的索引值,然后再设置ListBox.itemindex值就可以了
procedure TForm1.ListBox1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
begin
listbox1.ItemIndex:=Listbox1.ItemAtPos(Point(X,Y),True);
end;

5,379

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧