Impactnetworking


Borland C ++ Builder FAQ
The content The last update: 14.02.2004

Components - dynamic creation

For example, so:
for (int i=0; i <5; i ++) 
        { 
        TEdit *edit = new TEdit (this); 
        edit-> Parent = this; 
        edit-> Name = "edit" +AnsiString (i); 
        edit-> Height = 16; 
        edit-> Width = 104; 
        edit-> Left = 0; 
        edit-> Top = i*edit-> Height; 
        edit-> Text = AnsiString (i); 
        }
This code creates 5 TEdit'ов.
Обращатся to them it is possible so (by name):
for (int i=0; i <5; i ++) 
        { 
        TEdit *edit = (TEdit *) FindComponent ("edit" +AnsiString (i)); 
        edit-> Text = "www.sources.ru"; 
        }
Or absolutely other variant - array usage:
TEdit *edit [5]; 

for (int i=0; i <5; i ++) 
        { 
        edit [i] = new TEdit (this); 
        edit [i]-> Parent = this; 
        edit [i]-> Name = "edit" +AnsiString (i); 
        edit [i]-> Height = 16; 
        edit [i]-> Width = 104; 
        edit [i]-> Left = 0; 
        edit [i]-> Top = i * edit [i]-> Height; 
        edit [i]-> Text = AnsiString (i); 
        }

for (int i=0; i <5; i ++) 
        { 
        edit [i]-> Text = "www.sources.ru"; 
        } 

Lines (AnsiString) - the general questions

The author: trainer
The initial link: - - -

To begin with brief information
AnsiString - a class of a dynamic line in capacity to 232-1 characters. As basis for creation of this class served паскалевский type String which has been expanded according to C possibilities ++.
Its singularity is that two copies of this class can physically occupy the same section of storage. The copy of this class contains the counter of links to it when this counter is nullified, the copy is automatically destroyed. If to try to change a copy to which it is had more than one reference the new copy of this line which will change will be created. It reduces портебность programs in storage, but also reduces processing rate.
Heritage of Pascal is also that characters in it are numbered with 1, instead of with 0 as it is accepted in a C/C ++

Creation of a line
Thanks to presence of the overloaded designers the line can be created from a considerable quantity of basic types of language of a C ++, for example:
AnsiString empty_string;	/* blank line */ 
AnsiString one = 1;		/* the line one will contain "1" */ 
AnsiString one_and_half = 1.5;/* in one_and_half the line "1.5" */contains 
AnsiString one_charachter = ' a ';/* in one_charachter the line "a" */contains 
AnsiString dup_string = AnsiString:: StringOfChar (' A ', 5);/* dup_string receives value "AAAAA" */ 
AnsiString simply_string = "string"; 
AnsiString shorten ("the Long line", 7);/* in shorten the first 7 */characters get only 

Obtaining of the character from a line
To receive the character from a line of type AnsiString it is possible by means of the index operator:
AnsiString ansi_string = "Line"; 
char character = ansi_string [1];/* the very first */character is received 
ansi_string [6] = ' and ';		/* 6th is replaceable on a letter ' and ' */ 

Obtaining of the character array containing value AnsiString
Functions of library of execution of a C and function WinAPI demand, that lines were transferred to them in the form of character arrays. In AnsiString functions c_str and data are for this purpose intended. A difference in between that for blank line c_str returns "", and data - NULL. This difference can be illustrated by a following example:
AnsiString ansi_string; 
char * str1 = ansi_string.c_str (); 
char * str2 = (char *) ansi_string.data (); 

Copying of value AnsiString in character array Unicode
Function WideChar is for this purpose intended, and the size of a demanded array can be learned, used function WideCharBufSize
AnsiString ansi_string = "the Line AnsiString";/* our line */ 
wchar_t * UnicodeString = new wchar_t [ansi_string. WideCharBufSize ()];/* an array-receiver */ 
ansi_string. WideChar (UnicodeString, ansi_string. WideCharBufSize ()); 
//it is used the received line, then it is deleted 
delete [] UnicodeString; 

How to learn, how many characters in line?
For this purpose it is necessary to use function Length.

How to delete in line end gaps?
It is done by functions Trim, TrimLeft and TrimRight according to both ends, only from the beginning and only from the end.

Operation with fragments of a line
AnsiString предоставлякт possibility поиcка (Pos), insertions (Insert), removals (Delete) and copy derivings (SubString) a text fragment in line:
AnsiString ansi_string = "Line"; 
int index = ansi_string. Pos ("ка");/* we find the word termination */ 
if (index! = 0) { 
   ansi_string. Insert ("чеч", index);/* now the line looks like "Strochechka" */ 
   ansi_string. Delete (index, 2);	/* and now - "Line" */ 
}

How to reserve to a line the certain size
Sometimes happens it is necessary to reserve for a line space, sufficient for storage of a certain amount of characters. To make it it is possible by means of function SetLength