In most cases I am able to convert Delphi to C++, but this one gives me some headache. Maybe some of you could help.
As seen in this link here, which references some new functions on TListView in Embarcadero (FMX). As I am much more comfortable with C++ than Delphi I use C++Builder. In most cases this is quite Ok to translate and understand, and find workarounds. But here I am stuck:
procedure TForm1.FormCreate(Sender: TObject);
I: Integer;
begin
// ListView1 uses a classic Appearance
for I in [0..63] do
with ListView1.Items.Add do
begin
Text := Format('%d pages', [1000 + Random(1234567)]);
Detail := Format('%d kg of paper', [1000 + Random(1234)]);
ImageIndex := Random(ImageList1.Count);
end;
// ListView4 uses a dynamic appearance with items named
// Text1, Detail1, Portrait
for I in [0..63] do
with ListView4.Items.Add do
begin
Data['Text1'] := Format('%d pages', [1000 + Random(1234567)]);
Data['Detail1'] := Format('%d kg of paper', [1000 + Random(1234)]);
Data['Portrait'] := Random(ImageList1.Count);
end;
end;
end.
The section I am struggling with is
with ListView4.Items.Add do
begin
Data['Text1'] := Format('%d pages', [1000 + Random(1234567)]);
Data['Detail1'] := Format('%d kg of paper', [1000 + Random(1234)]);
Data['Portrait'] := Random(ImageList1.Count);
end;
How is this translated, or is this functionality which simply doesn't exists in c++ ?
When you want to add an item to a ListView you need to first create an item object (TListViewItem*) using Add() function that is child of TListView's Items property. Then, the Data property of item is expecting TValue, so you need to get TValue from a string or something else you want to put in the item. Remember to use BeginUpdate() before fragment where you are adding items to ListView and EndUpdate() after to improve the performance of this operation.
ListView4->BeginUpdate();
TListViewItem* item = ListView4->Items->Add();
UnicodeString string1 = "content of the String";
item->Data["Text1"] = TValue::From<UnicodeString>(string1);
item->Data["Detail1"] = TValue::From<UnicodeString>(string1);
item->Data["visitTime"] =TValue::From<int>(Random(ImageList1->Count))
ListView4->EndUpdate();