ListBox有多个相同内容时怎么返回每个item的index
lst1->Items->IndexOf(),这个只能返回最前面那个,怎么继续找第二个,第三个...
[解决办法]
IndexOf只能返回第一行,
要实现你的要求,可以另写函数,逐行遍历判断
[解决办法]
for(int i=0;i<ListBox1->Count;i++){
String tt=ListBox1->Items->Strings[i];
}
[解决办法]
int __fastcall CrnIndexsOfStrings(TStrings *lst, String str, TStrings *result)
{
result->Clear();
for (int i = 0; i < lst->Count; i++)
{
if (!CompareStr(lst->Strings[i], str))
result->Add(i);
}
return result->Count;
}
void __fastcall TForm1::Button1Click(TObject *Sender)
{
CrnIndexsOfStrings(ListBox1->Items, "aaa", ListBox2->Items);
}
#include <vector>
using namespace std;
void __fastcall TForm1::Button1Click(TObject *Sender)
{
vector<int> lbs;
vector<int>::iterator pointer;
TStrings *lbi = ListBox1->Items;
lbs.clear();
for (int i = 0; i < lbi->Count; i++)
if (!CompareStr(lbi->Strings[i], "Target"))
{
// i
[解决办法]
index
lbs.push_back(i);
}
// lbs.size()
[解决办法]
count
for (pointer = lbs.begin(); pointer != lbs.end(); pointer++)
{
// *pointer
[解决办法]
index
ListBox2->Items->Add((int)*pointer);
}
}