如何鼠标在listbox上移动的时候,会自动选择项目;
当项目的宽度大于listbox宽度的时候,能让它自动分行吗?

解决方案 »

  1.   

    对于第一个问题,在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;
      

  2.   

    对于第二个问题,由于ListBox没有像WordWrap之类的属性,所以不能直接实现你要求的功能,如果一定要这么做,只能用别的控件或自己写更多的代码了,如果自己写,实现方法我也不知道怎么办
      

  3.   

    自己写OnMouseMove事件实现选择某个项目;
    自己写OnDrawItem和OnMeasureItem事件,且Style设置为lbOwnerDrawVariable
      

  4.   

    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;
      

  5.   

    更正:
    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;
      

  6.   

    上面程序实现了:
        当项目的宽度大于listbox宽度的时候,能让它自动分行