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

相关推荐
Owen_Q11 分钟前
Denso Create Programming Contest 2025(AtCoder Beginner Contest 413)
开发语言·算法·职场和发展
·云扬·28 分钟前
【Java源码阅读系列37】深度解读Java BufferedReader 源码
java·开发语言
liulilittle1 小时前
C++ i386/AMD64平台汇编指令对齐长度获取实现
c语言·开发语言·汇编·c++
Thomas_YXQ2 小时前
Unity URP法线贴图实现教程
开发语言·unity·性能优化·游戏引擎·unity3d·贴图·单一职责原则
Zz_waiting.2 小时前
Javaweb - 10.4 ServletConfig 和 ServletContext
java·开发语言·前端·servlet·servletconfig·servletcontext·域对象
Touper.2 小时前
JavaSE -- 泛型详细介绍
java·开发语言·算法
sun0077002 小时前
std::forward作用
开发语言·c++·算法
Zhen (Evan) Wang3 小时前
(豆包)xgb.XGBRegressor 如何进行参数调优
开发语言·python
虾球xz3 小时前
CppCon 2018 学习:THE MOST VALUABLE VALUES
开发语言·c++·学习
阿蒙Amon4 小时前
C#扩展方法全解析:给现有类型插上翅膀的魔法
开发语言·c#