【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源码下载

相关推荐
swordbob17 分钟前
CAP 定理:为什么不能同时实现 C、A、P?
开发语言·后端·spring
疯狂成瘾者18 分钟前
Java 常用工具包 java.util
java·开发语言·windows
枫叶丹418 分钟前
【HarmonyOS 6.0】MDM Kit 新特性:PC/2in1设备无锁屏密码重启自动解锁能力详解
开发语言·华为·harmonyos
ZHW_AI课题组39 分钟前
Python 调用百度智能云 API 实现地址识别
开发语言·人工智能·python·机器学习·百度·数据挖掘
88号技师1 小时前
2026年2月一区SCI-交叉传播优化算法Propagation Alongside Crossover-附Matlab免费代码
开发语言·算法·数学建模·matlab·优化算法
A.零点1 小时前
【2个月 C 语言从入门到精通:零基础系统教程】第十二讲:深入了解指针(五)
c语言·开发语言·网络·笔记·visual studio
飞天狗1111 小时前
零基础JavaWeb入门——第五课第一小节:九大内置对象 · 第1个:request(请求对象)
java·开发语言·前端·后端·servlet
z落落1 小时前
C#ToolStrip+StatusStrip 状态栏实时显示系统时间+NotifyIcon系统托盘
开发语言·c#
插件开发1 小时前
vs2015 cuda c++ 线程号的计算详解
开发语言·c++·算法
石山代码1 小时前
变量与解构
开发语言·前端·javascript