【问题标题】:IsChecked property in Firemonkey TListbox not returning correct result?Firemonkey TListbox 中的 IsChecked 属性没有返回正确的结果?
【发布时间】:2012-03-26 17:20:54
【问题描述】:

我正在尝试将 IsChecked 和 IsSelected 与 FMX TListbox 控件结合使用,并且两者都返回不正确的结果(无论用户选择如何,都始终为 True)。这是一个已知的错误,是否有解决方法?

我的测试代码很简单:


var
    i: integer;
    lb: TListboxitem;

for i:=0 to lbxPartners.items.Count-1 do
begin

  lb :=tlistboxitem(lbxPartners.Items[i]);
  if lb=nil then continue;

  if lb.IsChecked then
     memo1.Lines.Add('item '+inttostr(i) +' checked')
  else
     memo1.Lines.Add('item '+inttostr(i)+' unchecked');
end;

【问题讨论】:

  • 应该是 lbxPartners.ListItems[i]。
  • Mike,谢谢你 - 它现在可以工作了,虽然我发现如果我想获取项目的文本,我仍然需要转到 .items 字符串列表; listitems[n].text 引发错误。哦,好吧,至少我现在可以正常工作了!
  • 我已经为你写了一个完整的答案。 ListItems[n].Text 对我来说很好用。你得到什么错误?有代码示例吗?

标签: delphi delphi-xe2 firemonkey


【解决方案1】:

TListBox.Items 是一个字符串列表,主要是为了让您可以像使用 VCL TListBox 一样使用控件。

TListBox.ListItems 是 TListBoxItems 的列表,它们是由 TListBox 显示的子控件。

访问 IsChecked 属性:

ListBox1.ListItems[n].IsChecked := True;

您可以使用以下任一方式访问文本:

ListBox1.Items[n] := 'Hello';
ListBox1.ListItems[n].Text := 'World';

您的完整代码将是(注意不需要演员表):

var i: integer; lb: TListboxitem;

for i:=0 to lbxPartners.items.Count-1 do begin
  lb := lbxPartners.ListItems[i];
  if lb=nil then continue;

  if lb.IsChecked then
     memo1.Lines.Add('item '+inttostr(i) +' checked')
  else
     memo1.Lines.Add('item '+inttostr(i)+' unchecked');
end;

【讨论】:

  • 不应该 ListBox1.Items[n].IsChecked := True;是 ListBox1.ListItems[n].IsChecked := True; ?
  • 是的,只有 listitems[i].ischecked 对我有用(Delphi 10.4)
猜你喜欢
  • 1970-01-01
  • 2014-09-13
  • 2012-08-01
  • 2014-05-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-14
相关资源
最近更新 更多