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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Assign a caption to a label through a variable

Status
Not open for further replies.

davman2002

Programmer
Nov 4, 2002
75
0
0
US
I need to know if it is possible to assign a value to a label using a variable. For example I would like to be able to create a function that will except an int and if that integer is 1 then the caption of Label1 can be assigned. here is my code

Thanks

void __fastcall TfrmMain::ShowPosition(int VPos, int HPos,int lblNum)
{
if (HPos == 5)
if (VPos == 1)
Label + lblNum->Caption = "UP";
else
Label + lblNum->Caption = "DOWN";
else
if (HPos == 3)
Label + lblNum->Caption = "LEFT";
else
Label + lblNum->Caption = "RIGHT";
}
 
I don't know what you are asking but i can tell you that this statement:
Label + lblNum->Caption = "UP";
is not legal since Label + lblNum->Caption is not a lvalue.

You must have an lvalue on the left side of an assignment operator.

-pete
 
What I am trying to do is -- instead of writing

Label1->Caption = "UP";
Label2->Caption = "UP";
Label3->Caption = "UP";
Label4->Caption = "UP";
Label5->Caption = "UP";
Label6->Caption = "UP";
Label7->Caption = "UP";
Label8->Caption = "UP";
Label9->Caption = "UP";
Label10->Caption = "UP";
Label11->Caption = "UP";
Label12->Caption = "UP";
Label13->Caption = "UP";


I would rather pass a variable to a function and then just write

Label + lblNum->Caption = "UP";
 
I got my answer here is what I did

void __fastcall TfrmMain::ShowPosition(int VPos, int HPos,int lblNum)
{
TLabel *Labels[10];

Labels[0] = lblLabel1;
Labels[1] = lblLabel2;
Labels[2] = lblLabel3;
Labels[3] = lblLabel4;
Labels[4] = lblLabel5;
Labels[5] = lblLabel6;
Labels[6] = lblLabel7;
Labels[7] = lblLabel8;

if (HPos == 5)
if (VPos == 1)
Labels[lblNum]->Caption = "UP";
else
Labels[lblNum]->Caption = "DOWN";
else
if (HPos == 3)
Labels[lblNum]->Caption = "LEFT";
else
Labels[lblNum]->Caption = "RIGHT";
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top