【Delphi 基础知识 29】ListBox控件的详细使用

在Delphi中,TListBox是一个常用的控件,用于显示项目列表。以下是TListBox的一些常见用法:

  1. 添加项目:

    使用 ListBox.Items.Add 方法可以向列表框中添加项目。例如:

    pascal 复制代码
    ListBox1.Items.Add('Item 1');
    ListBox1.Items.Add('Item 2');
  2. 删除项目:

    使用 ListBox.Items.Delete 方法可以删除指定位置的项目。例如:

    delphi 复制代码
    ListBox1.Items.Delete(0); // 删除第一个项目
  3. 清空列表:

    使用 ListBox.Items.Clear 方法可以清空列表框中的所有项目。例如:

    delphi 复制代码
    ListBox1.Items.Clear;
  4. 获取所选项目:

    你可以通过 ListBox.ItemIndex 属性获取用户当前选择的项目的索引。然后,你可以使用该索引来获取所选项目的值。例如:

    delphi 复制代码
    if ListBox1.ItemIndex <> -1 then
      ShowMessage('Selected item: ' + ListBox1.Items[ListBox1.ItemIndex]);
  5. 多选模式:

    ListBox可以配置为允许多选。你可以通过设置 ListBox.MultiSelect 为True启用多选模式。在多选模式下,可以使用 ListBox.Selected[] 属性来检查或设置所选项目。例如:

    delphi 复制代码
    ListBox1.MultiSelect := True;
    // 设置选中项
    ListBox1.Selected[0] := True;
    ListBox1.Selected[2] := True;
  6. 事件处理:

    ListBox具有多种事件,你可以利用这些事件来响应用户操作,如点击、双击、选择等。常见的事件包括 OnClickOnDblClickOnSelect 等。

  7. 自定义项目样式:

    你可以通过设置 ListBox.Style 属性为 lbOwnerDrawFixedlbOwnerDrawVariable 来自定义项目的外观。然后,在 OnDrawItem 事件中绘制每个项目的外观。

  8. item的拖拽:

    首先确保ListBox的 DragMode 属性设置为 dmAutomatic,以便启用自动拖拽模式。

    javascript 复制代码
    procedure TForm1.ListBox1DragOver(Sender, Source: TObject; X, Y: Integer;
      State: TDragState; var Accept: Boolean);
      var
      Idx: Integer;
      Pos: TPoint;
    begin
      Accept := False;
      if Sender is TListBox then
      with Sender as TListBox do
      begin
        Pos.X := X;
        Pos.y := Y;
        Idx := ItemAtPos(Pos, True);
        if (Idx > -1) and (idx <> ItemIndex) then
          Accept := True;
      end;
    end;
    
    procedure TForm1.ListBox1DragDrop(Sender, Source: TObject; X, Y: Integer);
    var
      Idx: Integer;
      Pos: TPoint;
    begin
      if Sender is TListBox then
      begin
        with Sender as TListBox do
        begin
          Pos.X := X;
          Pos.y := Y;
          Idx := ItemAtPos(Pos, True);
          Items.Move(ItemIndex, Idx);
          ItemIndex := Idx;
        end;
      end;
    end;
  9. item的上下移动:

    javascript 复制代码
    procedure LbMoveItemUp(AListBox: TListBox);
    var
      CurrIndex: Integer;
    begin
      with AListBox do
        if ItemIndex > 0 then
        begin
          CurrIndex := ItemIndex;
          Items.Move(ItemIndex, (CurrIndex - 1));
          ItemIndex := CurrIndex - 1;
        end;
    end; // Move an item down
    
    procedure LbMoveItemDown(AListBox: TListBox);
    var
      CurrIndex, LastIndex: Integer;
    begin
      with AListBox do
      begin
        CurrIndex := ItemIndex;
        LastIndex := Items.Count;
        if ItemIndex <> -1 then
        begin
          if CurrIndex + 1 < LastIndex then
          begin
            Items.Move(ItemIndex, (CurrIndex + 1));
            ItemIndex := CurrIndex + 1;
          end;
        end;
      end;
    end;
    
    procedure TForm1.Button6Click(Sender: TObject);
    begin
      LbMoveItemUp(ListBox1);
    end;
    
    procedure TForm1.Button7Click(Sender: TObject);
    begin
      LbMoveItemDown(ListBox1);
    end;

Listbox使用Demo源码下载

相关推荐
艾莉丝努力练剑32 分钟前
【LeetCode&数据结构】单链表的应用——反转链表问题、链表的中间节点问题详解
c语言·开发语言·数据结构·学习·算法·leetcode·链表
倔强青铜35 小时前
苦练Python第18天:Python异常处理锦囊
开发语言·python
u_topian5 小时前
【个人笔记】Qt使用的一些易错问题
开发语言·笔记·qt
珊瑚里的鱼5 小时前
LeetCode 692题解 | 前K个高频单词
开发语言·c++·算法·leetcode·职场和发展·学习方法
AI+程序员在路上6 小时前
QTextCodec的功能及其在Qt5及Qt6中的演变
开发语言·c++·qt
xingshanchang6 小时前
Matlab的命令行窗口内容的记录-利用diary记录日志/保存命令窗口输出
开发语言·matlab
Risehuxyc6 小时前
C++卸载了会影响电脑正常使用吗?解析C++运行库的作用与卸载后果
开发语言·c++
AI视觉网奇6 小时前
git 访问 github
运维·开发语言·docker
不知道叫什么呀6 小时前
【C】vector和array的区别
java·c语言·开发语言·aigc
liulilittle6 小时前
.NET ExpandoObject 技术原理解析
开发语言·网络·windows·c#·.net·net·动态编程