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

相关推荐
CodeCraft Studio4 小时前
3D文档控件Aspose.3D实用教程:使用 C# 构建 OBJ 到 U3D 转换器
开发语言·3d·c#·3d渲染·aspose·3d文件格式转换·3d sdk
superlls4 小时前
(Redis)主从哨兵模式与集群模式
java·开发语言·redis
chenglin0165 小时前
C#_gRPC
开发语言·c#
骑驴看星星a5 小时前
数学建模--Topsis(Python)
开发语言·python·学习·数学建模
tju新生代魔迷7 小时前
C语言宏的实现作业
c语言·开发语言
yueyuebaobaoxinx8 小时前
MATLAB 与 Simulink 联合仿真:控制系统建模与动态性能优化
开发语言·matlab·性能优化
superlls9 小时前
(计算机网络)JWT三部分及 Signature 作用
java·开发语言·计算机网络
一只鲲9 小时前
56 C++ 现代C++编程艺术5-万能引用
开发语言·c++
liulilittle10 小时前
.NET反射与IL反编译核心技术
开发语言·数据库·c#·.net·反射·反编译·il
扛麻袋的少年10 小时前
6.Kotlin的Duration类
android·开发语言·kotlin