delphi - 将 TListBox 选定行的内容存储在变量上

标签 delphi list listview variables lazarus

我正在 Lazarus 上开发一个电子书管理器,但我在使用一个我从未使用过的组件 (TListBox) 时遇到了一些麻烦。在名为 CategoryListTListBox 上,我有这些项目:

Literature and Fiction
Romance
Computers and Internet
Poetry
Professional and Technical
Science Fiction and Fantasy
Biographies and Memoirs
Business and Finance
Children's Books
Entertainment
History
Science
Self-Help
Textbooks and Educational Materials
Travel
Westerns

当用户在 CategoryList 上选择一个项目时,我想将它存储在一个变量中,但是我该怎么做呢?

最佳答案

如果你想要索引:

index := CategoryList.ItemIndex;

如果你想要字符串:

str := CategoryList.Items[CategoryList.ItemIndex];

要捕捉用户选择某物的瞬间,您需要注册一个 OnChange 事件:

CategoryList.OnChange := CategoryListChange;

其中 CategoryListChange 是一个事件监听器:

procedure TMyForm.CategoryListChange(Sender: TObject);
begin
  // do something with CategoryList.Items[CategoryList.ItemIndex]
end; 

您可能想查看 this webpage 上的一些帖子也是!

关于delphi - 将 TListBox 选定行的内容存储在变量上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1972607/

相关文章:

delphi - 将参数从 Delphi 5 传递到 Delphi DLL XE

python - 在 Python 中按不同级别排序

列表重新格式化中的 Python 列表

android - ListView 没有显示在 fragment 中?

android - 防止在 ListView 中双重选择

java - ListItem.onItemClickListener 与 CustomAdapter 无响应

delphi - GetAsyncKeyState "strange"行为

delphi - 当系统文件实际存在时找不到它

delphi - Delphi 单元名称的 XMLDoc

python - python 将 RGB 转换为灰度