简体   繁体   中英

Is there a way to convert this delphi code to c++builder

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++ ?

With kind of introduces an unnamed variable and a scope for it. In C++ you have to be explicit. The Delphi fragment is equivalent to

var
  li: TListItem;
begin
  li := ListView4.Items.Add;
  li.Data['Text1'] := Format('%d pages', [1000 + Random(1234567)]);
  li.Data['Detail1'] := Format('%d kg of paper', [1000 + Random(1234)]);
  li.Data['Portrait'] := Random(ImageList1.Count);
end;

(if I didn't mess up :-)).

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();

Try something like this:

// Never use the OnCreate event in C++,
// use the class constructor instead...
__fastcall TForm1::TForm1(TComponent *Owner)
    : TForm(Owner)
{
    // ListView1 uses a classic Appearance
    for(int i = 0; i < 64; ++i)
    {
        TListViewItem *Item = ListView1->Items->Add();
        Item->Text = Format(L"%d pages", ARRAYOFCONST(( 1000 + Random(1234567) )) );
        Item->Detail = Format(L"%d kg of paper", ARRAYOFCONST(( 1000 + Random(1234) )) );
        Item->ImageIndex = Random(ImageList1->Count);
    }

    // ListView4 uses a dynamic appearance with items named
    // Text1, Detail1, Portrait
    for(int i = 0; i < 64; ++i)
    {
        TListViewItem *Item = ListView4->Items->Add();
        Item->Data[L"Text1"] = Format(L"%d pages", ARRAYOFCONST(( 1000 + Random(1234567) )) );
        Item->Data[L"Detail1"] = Format(L"%d kg of paper", ARRAYOFCONST(( 1000 + Random(1234) )) );
        Item->Data[L"Portrait"] = Random(ImageList1->Count);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM