如何在不使用所选项目的情况下获取其子项目等于某个字符串的listview项目的索引?

Del*_*ent -1 delphi listview delphi-xe7

我目前使用的列表视图我的项目中我想通过寻找其子项字符串来获得一些项目的指标,我有项目和子项目列表视图,item caption := namesubitem := id我想找到这个项目,其中的指数sub item := id,我怎么能做到这一点,我搜索虽然对于某些方程而且还没有.我需要这个的原因是因为subitem id有唯一的id而且这个很安全,而不是按标题使用find item

Ken*_*ite 7

您需要遍历列表视图Items,查看要匹配的正确子项.例如,给定一个TListView有三列(A,B和C),搜索B列以查找内容:

function TForm1.FindListIndex(const TextToMatch: string): Integer;
var
  i: Integer;
begin
  for i := 0 to ListView1.Items.Count - 1 do
    if ListView1.Items[i].SubItems[1] = TextToMatch then
      Exit(i);
  Result := -1;
end;
Run Code Online (Sandbox Code Playgroud)

当然,替换你自己的匹配函数(例如,SameText):

if SameText(ListView1.Items[i].SubItems[1], TextToMatch) then
   ...;
Run Code Online (Sandbox Code Playgroud)

如果要在任何子项中搜索匹配项,只需要一个嵌套循环:

function TForm1.FindListIndex(const TextToMatch: string): Integer;
var
  i, j: Integer;
begin
  for i := 0 to ListView1.Items.Count - 1 do
    for j := 0 to ListView1.Items[i].SubItems.Count - 1 do
      if ListView1.Items[i].SubItems[j] = TextToMatch then
        Exit(i);
  Result := -1;
end;
Run Code Online (Sandbox Code Playgroud)