在一個android應用程式中,在一個ListBox中,我正在創建ListBoxItems,在每個Item中創建一個reactangle,在每個矩形中,我在運行時動態處理5個標簽,為每個創建的Listboxitem創建5個標簽。標簽的唯一唯一屬性是為創建的每個串列框項更改的名稱。現在,我找不到找到特定標簽來更改其文本屬性的方法。我可以在哪個 Listboxitem 中找到并通過擴展找到哪個矩形是我想要的標簽(帶有 tag_num 變數),但我無法確定更改其文本的特定標簽。
創建 Listboxitems 的代碼
...
lbim := TListBoxItem.Create(ListBox1);
lbim.Parent := ListBox1;
lbim.TextSettings.Font.Size := 12;
lbim.StyledSettings:=
[TStyledSetting.Family,TStyledSetting.Style,TStyledSetting.FontColor];
lbim.Text := LJsonValue1_2.Value;
lbim.Height := 50;
lbim.Margins.Top := 2;
rctg := TRectangle.Create(lbim);
rctg.Parent := lbim;
rctg.Align := TAlignLayout.Client;
rctg.Stroke.Kind := TBrushKind.Solid;
rctg.Stroke.Color := $FF51ABAE;
rctg.Stroke.Thickness := 1;
rctg.Tag := tag_num;
rctg.OnClick:=QuantityClick;
rctg.Margins.Left:=2;
rctg.Margins.Right:=2;
rctg.Index := -5;
lb := TLabel.Create(rctg);
lb.Parent := rctg;
lb.Align := TAlignLayout.Center;
lb.TextSettings.HorzAlign := TTextAlign.Leading;
lb.Width := 150;
lb.Name := 'value0_' IntToStr(tag_num);
lb.TextSettings.Font.Size := 12;
lb.StyledSettings:=
[TStyledSetting.Family,TStyledSetting.Style,TStyledSetting.FontColor];
lb.Margins.Right := 180;
lb.Margins.Bottom := 35;
lb.Text := LJsonValue1_1.Value;
lb1 := TLabel.Create(rctg);
lb1.Parent := rctg;
lb1.Align := TAlignLayout.Center;
lb1.TextSettings.HorzAlign := TTextAlign.Trailing;
lb1.Width := 150;
lb1.Name := 'value1_' IntToStr(tag_num);
lb1.TextSettings.Font.Size := 12;
lb1.StyledSettings:=
[TStyledSetting.Family,TStyledSetting.Style,TStyledSetting.FontColor];
lb1.Margins.Bottom := 35;
lb1.Margins.left := 180;
lb1.Text := LJsonValue4_2.Value;
...
tag_num:=tag_num 1;
等等...可以從 1 個 ListBoxItem 創建到 15 個或更多.. 如何更改,例如第 10 個 lIstBoxItem 中的 lb1 標簽,其文本?我在這里發現了一個類似的問題Search for a Label by its Caption 但對我不起作用..有什么想法嗎?
我的搜索代碼是:
for i := 0 to ListBox1.Items.Count - 1 do
begin
item := Form1.ListBox1.ListItems[i];
if item.Components[i] is TLabel then
begin
if TLabel(item.Components[i]).Name = 'value1' then
begin
if ed_quant1.Text = '' then
begin
ed_quant1.Text := '0';
TLabel(item.Components[i]).Text := ed_quant1.Text;
end
else
begin
TLabel(item.Components[i]).Text := ed_quant1.Text;
end;
end;
if TLabel(item.Components[i]).Name = 'value2' then
begin
if ed_quant2.Text = '' then
begin
ed_quant2.Text := '0';
TLabel(item.Components[i]).Text := ed_quant1.Text;
end
else
begin
TLabel(item.Components[i]).Text := ed_quant1.Text;
end;
end;
end;
end;
我應該提到,搜索發生在創建 ListBox 的另一種形式中。
uj5u.com熱心網友回復:
在提出這樣的問題時,最好給出代碼的簡化版本,因為這樣更容易理解。但是,我認為以下內容應該很有用。(我只在 Windows 上試過這個,而不是 Android,但我不希望這會有所作為。)
首先,您的搜索代碼存在一個問題,即您使用相同的回圈變數 ( i) 來獲取兩個不同的控制元件。也就是說,當您獲得第一個專案時,您正在尋找該專案中的第一個組件,而在第二個專案中,您正在尋找第二個組件,依此類推。您需要單獨的回圈變數。
只要您正在搜索正確的串列框,您正在從另一個表單搜索應該沒有什么區別。
我沒有嘗試使用該.Components[]屬性,因為它可以包含非控制元件組件,而您只想要TLabels,它們是控制元件。但是,我不明白為什么這也不行。
我下面的代碼不會復制您確切的搜索和替換目標,因此您當然必須對其進行調整。在我的中,我將標簽命名為串列項中的第 n 個標簽'value0_' n.ToString。n所以你必須相應地調整它。
procedure TForm1.ChangeLabels;
var Item: TListBoxItem;
i,j: Integer;
Ctrl: TControl;
Lbl: TLabel absolute Ctrl;
begin
for i:=0 to ListBox1.Count-1 do begin
Item:=ListBox1.ListItems[i];
for j:=0 to Item.ControlsCount-1 do begin
Ctrl:=Item.Controls[j];
if Ctrl is TLabel then begin
if Lbl.Name='value0_3' then Lbl.Text:='CHANGED IT!';
if Lbl.Name='value0_4' then Lbl.Text:='Another one!';
end;
end;
end;
end;
uj5u.com熱心網友回復:
這實際上很簡單,但是您的方法似乎比應有的復雜得多。
當您動態創建 TlistboxItem 然后創建 TLabel 并將您創建的 TLisboxItem 設定為所有者時,標簽不需要唯一的名稱。它需要對專案是唯一的,但不是全域的。
示例:您不能在同一個專案中動態創建 2 個具有相同名稱的標簽,但您可以在 2 個不同的專案中使用相同的名稱。
使用標簽創建新串列框專案的示例:
// Creating New Items
lst1.BeginUpdate;
try
for var I := 0 to 100 do
begin
// Create Listbox Item
var
aItem := TListBoxItem.Create(lst1);
aItem.Parent := lst1;
aItem.Height := 50;
// Create label in listbox item
var
aLabel := TLabel.Create(aItem);
aLabel.Parent := aItem;
aLabel.Text := 'Test';
// Has to be unique when the same item is parent but not anywhere
// else so you can use the name "lblTest" over and over for each new item
aLabel.Name := 'lblTest';
end;
finally
lst1.EndUpdate;
end;
更改串列框項中的這些動態標簽的示例:
// Changing item labels
lst1.BeginUpdate;
try
for var I := 0 to lst1.Count - 1 do
begin
var
aItem := lst1.ListItems[I];
(aItem.FindComponent('lblTest') as TLabel).Text := 'Changed';
end;
finally
lst1.EndUpdate;
end;
編輯:
由于rctg是您標簽的父級,您將使用以下內容:
(rctg.FindComponent('lblTest') as TLabel).Text := 'Changed';
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/477382.html
下一篇:Delphi中的奇怪文本框架
