RAD Studio VCL Reference
|
Sets the strings in the list, and possibly associated objects, from a source object.
procedure Assign(Source: TPersistent); override;
virtual __fastcall Assign(TPersistent * Source);
Use Assign to set the value of the TStrings object from another object. If Source is of type TStrings, the list is set to the list of the source TStrings object, and if associated objects are supported, any associated objects are copied from the Source as well.
If Source is not of type TStrings, the inherited Assign will set the value of the list from any object that supports TStrings in its AssignTo method.
C++ Examples:
/* This example uses a tab control to display the contents of several files. To run the example, place a tab control on a form and add a memo control that fits into its client area. Be sure to leave enough room for the tabs when they appear. Then add an OpenDialog and a button to the form. The "Add a file" button adds a single file to the tabcontrol. The "Assign files" button removes previous files and can be used to assign multiple files. To assign multiple files, use CNTL Select or SHIFT Select to select files in the OpenDialog. */ void __fastcall TForm1::Add_a_fileClick(TObject *Sender) { OpenDialog1->Options << ofAllowMultiSelect << ofFileMustExist << ofHideReadOnly; if (OpenDialog1->Execute()) { int index = TabControl1->Tabs->Add(OpenDialog1->FileName); Memo1->Lines->LoadFromFile(TabControl1->Tabs->Strings[index]); TabControl1Change(Sender); } } void __fastcall TForm1::Assign_filesClick(TObject *Sender) { OpenDialog1->Options << ofAllowMultiSelect << ofFileMustExist << ofHideReadOnly; if (OpenDialog1->Execute()) { TabControl1->Tabs->Assign(OpenDialog1->Files); Memo1->Lines->LoadFromFile(TabControl1->Tabs->Strings[TabControl1->TabIndex]); } } /* Place the following code in the tab control’s OnChange event handler: */ void __fastcall TForm1::TabControl1Change(TObject *Sender) { Memo1->Lines->LoadFromFile( TabControl1->Tabs->Strings[TabControl1->TabIndex]); }
/* This example creates a stringlist, adds elements to it and assigns the strings to items of a combo box. */ #include <memory> //for STL auto_ptr class void __fastcall TForm1::Button1Click(TObject *Sender) { std::auto_ptr<TStrings> StringList(new TStringList()); StringList->Add("This example uses A string List. "); StringList->Add("It is the easiest way to add strings"); StringList->Add("to a combobox's list of strings. "); StringList->Add("Always remember TStrings is abstract,"); StringList->Add("So create a TStringList instead."); ComboBox1->Width = 210; ComboBox1->Items->Assign(StringList.get()); ComboBox1->ItemIndex = 0; }
Delphi Examples:
{ This example uses a tab control to display the contents of several files. To run the example, place a tab control on a form and add a memo control that fits into its client area. Be sure to leave enough room for the tabs when they appear. Then add an OpenDialog and a button to the form. The "Add a file" button adds a single file to the tabcontrol. The "Assign files" button removes previous files and can be used to assign multiple files. To assign multiple files, use CNTL Select or SHIFT Select to select files in the OpenDialog. } procedure TForm1.Add_a_fileClick(Sender: TObject); var index : integer; begin OpenDialog1.Options := [ofAllowMultiSelect, ofFileMustExist, ofHideReadOnly]; if OpenDialog1.Execute then begin index:= TabControl1.Tabs.Add(OpenDialog1.FileName); Memo1.Lines.LoadFromFile(TabControl1.Tabs[index]); TabControl1Change(Sender); end; end; procedure TForm1.Assign_filesClick(Sender: TObject); var index : integer; begin OpenDialog1.Options := [ofAllowMultiSelect, ofFileMustExist, ofHideReadOnly]; if OpenDialog1.Execute then begin TabControl1.Tabs.Assign(OpenDialog1.Files); Memo1.Lines.LoadFromFile( TabControl1.Tabs[TabControl1.TabIndex]); end; end; { Place the following code in the tab control’s OnChange event handler: } procedure TForm1.TabControl1Change(Sender: TObject); begin with TabControl1 do Memo1.Lines.LoadFromFile(Tabs[TabIndex]); end;
{ This example creates a stringlist, adds elements to it and assigns the strings to items of a combo box. } procedure TForm1.Button1Click(Sender: TObject); var StringList: TStringList; begin StringList := TStringList.Create; try with StringList do begin Add('This example uses A string List.'); Add('It is the easiest way to add strings'); Add('to a combobox''s list of strings.'); Add('Always remember TStrings.Create method'); Add('is abstract; So use TStringList.Create'); Add('method instead.'); end; with ComboBox1 do begin Width := 210; Items.Assign(StringList); ItemIndex := 0; end; finally StringList.free; end; end;
Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
|
What do you think about this topic? Send feedback!
|