Jump to content

C++/TStringList

From Wikiversity
< C++

The items of a list box are AnsiString objects created from the TStrings class. This allows you to use the properties of the TStrings class. The TStrings class is more useful if you know the list at design time. Sometimes you will not be able to predict this list. For example, the list can depend on a conditional or depending action. Borland does not allow you to create an instance of the TStrings class. The alternative is to use the STringsList class. To do this, use the new operator to create a dynamic list based on the TStringsList or the TList class, fill out the list and assign it to the TStrings::Items property. For example, to create a Date Time dialog box such as the one in WordPad, you cannot create the list at design time. Because the dates and times must be set by the computer at the exact time the user wants to insert a date or time on the document, you can create this list only WHEN the user wants it. You can implement the list in a dialog box and fill it up when the dialog opens, which is with the OnCreate() event of the dialog box:


void __fastcall TDateAndTime::FormCreate(TObject *Sender) {

   TDateTime TodayDate = Date();
   TDateTime RightNow = Time();
   TStringList* DateList = new TStringList;
   DateList->Add(TodayDate);
   ShortDateFormat = "m/d/yy";
   DateList->Add(TodayDate);
   ShortDateFormat = "m/dd/yy";
   DateList->Add(TodayDate);
   ShortDateFormat = "m/dd/yyyy";
   DateList->Add(TodayDate);
   ShortDateFormat = "yy/m/dd";
   DateList->Add(TodayDate);
   DateSeparator = '-';
   ShortDateFormat = "yyyy/m/dd";
   DateList->Add(TodayDate);
   ShortDateFormat = "dd/mmm/yy";
   DateList->Add(TodayDate);
   LongDateFormat = "dddd";
   DateSeparator = ',';
   ShortDateFormat = "dddd/ mmmm dd/ yyyy";
   DateList->Add(TodayDate);
   ShortDateFormat = "mmmm dd/ yyyy";
   DateList->Add(TodayDate);
   ShortDateFormat = "dddd/ dd mmmm/ yyyy";
   DateList->Add(TodayDate);
   ShortDateFormat = "dd mmmm/ yyyy";
   DateList->Add(TodayDate);
   ShortTimeFormat = "h:n:s";
   DateList->Add(RightNow);
   ShortTimeFormat = "hh:nn:ss";
   DateList->Add(RightNow);
   LongTimeFormat = "HH:nn:ss";
   DateList->Add(RightNow);
   lstFormats->Items->AddStrings(DateList);

} //---------------------------------------------------------------------------