Programming C++Builder Using TStringList in C++Builder

A sample to use TStringList in C++Builder. The TStringList is very useful in some cases to manage the text list or objects.

Create a new project, put a button on your form. Double click the button, assign the following codes in its onClick event handler.

void __fastcall TForm1::Button1Click(TObject *Sender)
    //Create a TStringList object and adding text list to it.
    TStringList *pList = new TStringList;
    pList->Add("Here is a sample");
    pList->Add("On how to use TStringList");
    pList->Add("We first create a new stringlist");
    pList->Add("then adding text list to it");
    pList->Add("What's the following thing?");
    pList->Add("Show the text statements one by one.");
    pList->Add("At last, we save it to a file.");
    pList->Add("under your root of partition c.");
    pList->Add("then delete this stringlist.");
    pList->Add("Thank you for visiting");

    //Show the list we have stored in the pList
    for (int i = 0; i Count; i++)

    //Save the list contents to a file

    //Delete the list from memory.
    delete pList;

The TStringList object can store not only strings, but also objects in its list. While store the object type of list, you need to use the addobject method, and you have to specify what kind of object it is when visit the list menbers. See below:

TButton *pBtn = new TButton(this);
pBtn->Caption = "list button";
pList->AddObject("my button", pBtn);
TButton *pButton = (TButton*)pList->Objects[0];
delete pBtn;

See more articles on C++Builder programs:

How to Start Program With Delphi/C++Builder? Delphi/C-Builder's Integrated Development Environment Introduction .
An example on using scanline to access bitmap pixels in C-Builder .
How to use an ini file in C++Builder .
How to convert an icon to a bitmap in C++Builder .
How to hide the 3D border of TPageControl in Delphi?
Using TBlobField and TBlobStream in C++Builder .
An example on how to use GetFormImage() and Assign() functions in C++Builder .
How to capture a web page's content and save it to an image .

Add comment

Security code

Programming - C++Builder