Delphi ListBoxItem可见错误
Delphi 10.2 Tokyo中的TListBox和TListBoxItem有一些我不理解的地方 当第一个字母更改时,我添加了一个TListBoxGroupHeader,一些值(TListBoxItem)将加载到我的列表框中Delphi ListBoxItem可见错误,delphi,firemonkey,visible,Delphi,Firemonkey,Visible,Delphi 10.2 Tokyo中的TListBox和TListBoxItem有一些我不理解的地方 当第一个字母更改时,我添加了一个TListBoxGroupHeader,一些值(TListBoxItem)将加载到我的列表框中 procedure TForm1.Button1Click(Sender: TObject); var lbItem: TListBoxItem; Letter: string; ListBoxGroupHeader: TListBoxGroupHea
procedure TForm1.Button1Click(Sender: TObject);
var
lbItem: TListBoxItem;
Letter: string;
ListBoxGroupHeader: TListBoxGroupHeader;
i: integer;
ListValue: TStringList;
begin
Letter := '';
ListValue := TStringList.Create;
try
ListValue.Add('Germany');
ListValue.Add('Georgie');
ListValue.Add('France');
ListValue.Add('Venezuela');
ListValue.Add('Poland');
ListValue.Add('Russia');
ListValue.Add('Sweden');
ListValue.Add('Denmark');
ListBox1.BeginUpdate;
for i := 0 to ListValue.Count - 1 do
begin
if Letter <> Copy(ListValue[i], 0, 1).ToUpper then
begin
ListBoxGroupHeader := TListBoxGroupHeader.Create(ListBox1);
ListBoxGroupHeader.Text := Copy(ListValue[i], 0, 1).ToUpper;
ListBox1.AddObject(ListBoxGroupHeader);
end;
lbItem := TListBoxItem.Create(ListBox1);
lbItem.Text := ListValue[i];
lbItem.Tag := i;
ListBox1.AddObject(lbItem);
Letter := Copy(ListValue[i], 0, 1).ToUpper;
end;
finally
ListBox1.EndUpdate;
FreeAndNil(ListValue);
end;
end;
if ListBox1.ListItems[i] is TListBoxGroupHeader then
ListBoxItem.Text := '>>' + ListBoxItem.Text + '<<'
第一个GroupHeader(字母G)始终可见!看起来在GroupHeader后面有一个ListBoxItem。。使用检查点时,Visible设置为false。。所以我不明白
如果我写字母“V”,我只看到带有字母“G”的GroupHeader
如果是GroupHeader,我甚至尝试更改文本值
procedure TForm1.Button1Click(Sender: TObject);
var
lbItem: TListBoxItem;
Letter: string;
ListBoxGroupHeader: TListBoxGroupHeader;
i: integer;
ListValue: TStringList;
begin
Letter := '';
ListValue := TStringList.Create;
try
ListValue.Add('Germany');
ListValue.Add('Georgie');
ListValue.Add('France');
ListValue.Add('Venezuela');
ListValue.Add('Poland');
ListValue.Add('Russia');
ListValue.Add('Sweden');
ListValue.Add('Denmark');
ListBox1.BeginUpdate;
for i := 0 to ListValue.Count - 1 do
begin
if Letter <> Copy(ListValue[i], 0, 1).ToUpper then
begin
ListBoxGroupHeader := TListBoxGroupHeader.Create(ListBox1);
ListBoxGroupHeader.Text := Copy(ListValue[i], 0, 1).ToUpper;
ListBox1.AddObject(ListBoxGroupHeader);
end;
lbItem := TListBoxItem.Create(ListBox1);
lbItem.Text := ListValue[i];
lbItem.Tag := i;
ListBox1.AddObject(lbItem);
Letter := Copy(ListValue[i], 0, 1).ToUpper;
end;
finally
ListBox1.EndUpdate;
FreeAndNil(ListValue);
end;
end;
if ListBox1.ListItems[i] is TListBoxGroupHeader then
ListBoxItem.Text := '>>' + ListBoxItem.Text + '<<'
如果ListBox1.ListItems[i]是TListBoxGroupHeader,则
ListBoxItem.Text:='>>'+ListBoxItem.Text+'我可以复制您所描述的内容,它与隐藏标题有关,同时保持标题下的项目可见。在这种情况下,应用程序显示的是标题而不是项目。我还没有检查里面有什么问题,但似乎不是你想要的。IMHO您希望保留与搜索文本匹配的可见项目及其各自的标题,并且仅隐藏标题下没有项目
如果是这样,请尝试以下方法:
procedure FilterItems(const Text: string; ListBox: TListBox);
var
I: Integer; { ← loop variable }
Hide: Boolean; { ← flag indicating if we want to hide the last header we passed }
Item: TListBoxItem; { ← currently iterated item }
Head: TListBoxGroupHeader; { ← last header item we passed during iteration }
begin
Head := nil;
Hide := True;
ListBox.BeginUpdate;
try
{ if search text is empty, show all items }
if Text.IsEmpty then
for I := 0 to ListBox.Content.ControlsCount - 1 do
ListBox.ListItems[I].Visible := True
else
{ otherwise compare text in non header items }
begin
for I := 0 to ListBox.Content.ControlsCount - 1 do
begin
Item := ListBox.ListItems[I];
{ if the iterated item is header }
if Item is TListBoxGroupHeader then
begin
{ set the previous header visibility by at least one visible item }
if Assigned(Head) then
Head.Visible := not Hide;
{ assume hiding this header and store its reference }
Hide := True;
Head := TListBoxGroupHeader(Item);
end
else
{ if the iterated item is a regular item }
if Item is TListBoxItem then
begin
{ set the item visibility by matching text; if the item remains visible, it
means we don't want to hide the header, so set our flag variable as well }
if Item.Text.ToLower.Contains(Text) then
begin
Hide := False;
Item.Visible := True;
end
else
Item.Visible := False;
end;
end;
{ the iteration finished, so now setup visibility of the last header we passed }
if Assigned(Head) then
Head.Visible := not Hide;
end;
finally
ListBox.EndUpdate;
end;
end;
procedure TForm1.Edit1ChangeTracking(Sender: TObject);
begin
FilterItems(Edit1.Text.Trim.ToLower, ListBox1);
end;
对存在错误(设置项目可见性时)。无论如何,我不认为这是你想做的。我认为您希望搜索国家,只显示那些与相应标题匹配的文本,这需要在非标题项中搜索,并隐藏那些没有标题项的标题。“这就是你想要实现的吗?”维多利亚谢谢你的回答,我尝试的一切都失败了。我决定用TVertScrollBox和TLayout创建我自己的菜单,在这里我创建了我的TRecangle。。这就是我的工作,我将在Embarcadero的bug追踪器上创建一个任务。