Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

control name instead of index

Status
Not open for further replies.

hennep

Programmer
Dec 10, 2000
429
How can I address a label on a form by its name ( instead of addressing by an index).

I have a form with 256 labels. I tried the code below but cbuilder cannot handle: Controls["name"];

void Refresh( void ) {
static unsigned short int iLabel = 0;
TLabel *c;
char buf1[12], buf2[12];
if( ++iLabel >= 256 ) {
iLabel = 0;
}
sprintf( buf1, "lblByte%02X", iLabel );
sprintf( buf2, "%02X", P->Value(iLabel) );
c = (TLabel*)this->Controls[buf1];
c->Caption = buf2;
}
 
Greetinx!

The following sample code shows how to find out 'n'th control (here label) on form:

//n is preinitialized variable (number of control)
TControl *__fastcall TForm1::LabelSearch(int n)
{
for(int j = 0; j < ControlCount; j++)
if(Controls[j]->Name == &quot;Label&quot;+AnsiString(n))
return Controls[j];
return NULL;
}
Happy programming!))
 
Thanks Pavlo,

I wonder if this method is quick enough. There are 256 labels on the form. executing the for loop 256 times was not what I wanted.

I came up with this solution:

TLabel *c;
c = (TLabel*) this->FindComponent(&quot;Name of Label&quot;);
c->Caption = &quot;Caption for Label&quot;;

I don't know how the findcomponent function works internally, it might do the same loop through all the controls.

When I can find some time I will do a performance test with both methods.

Hennie.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top