Hello! How to make a variable receive an increment in the name? I want to do it for example: I have: Label1->Caption = "1"; Label2->Caption = "2"; Label3->Caption = "3"; Label4->Caption = "4"; And I want to do something like that: for (i=1; i<5; i++) { Label+i->Caption = i; } Any idea? Can I do something like that? Thanks and regards Alexandre
![]() |
0 |
![]() |
Create a vector of pointers to the various objects. In your case below, construct a vector (called Labels) of pointers to TLable long enough to hold pointers to all your labels. Then, store the pointers to the labels in the vector. Labels[0] = &Label1; etc. Your loop will now become: for (i=1; i<5; i++) { Labels[i-1]->Caption = i; } You might want to allocate one extra so that you can address by: Labels[1] = &Label1; and Labels[i]->Caption = i; John "Alexandre Paulino Sierra da Silva" <alexandre@belatriz.com.br> wrote in message news:3650@forums.codegear.com... > Hello! > How to make a variable receive an increment in the name? > I want to do it for example: > > I have: > Label1->Caption = "1"; > Label2->Caption = "2"; > Label3->Caption = "3"; > Label4->Caption = "4"; > > And I want to do something like that: > > for (i=1; i<5; i++) > { > Label+i->Caption = i; > } > > Any idea? Can I do something like that? > > Thanks and regards > > Alexandre
![]() |
0 |
![]() |
Hi John, thank you for your answer. How to create this vector? is this the way? TLabel *Labels; Labels = new TLabel[3]; I'm getting the error "[BCC32 Error] UnitCadastroPedidoLancarCatalogo.cpp(126): E2248 Cannot find default constructor to initialize array element of type 'TLabel'" Thank you. "John Cibulskis" <jcibulskis@sbcglobal.net> escreveu na mensagem news:3651@forums.codegear.com... Create a vector of pointers to the various objects. In your case below, construct a vector (called Labels) of pointers to TLable long enough to hold pointers to all your labels. Then, store the pointers to the labels in the vector. Labels[0] = &Label1; etc. Your loop will now become: for (i=1; i<5; i++) { Labels[i-1]->Caption = i; } You might want to allocate one extra so that you can address by: Labels[1] = &Label1; and Labels[i]->Caption = i; John "Alexandre Paulino Sierra da Silva" <alexandre@belatriz.com.br> wrote in message news:3650@forums.codegear.com... > Hello! > How to make a variable receive an increment in the name? > I want to do it for example: > > I have: > Label1->Caption = "1"; > Label2->Caption = "2"; > Label3->Caption = "3"; > Label4->Caption = "4"; > > And I want to do something like that: > > for (i=1; i<5; i++) > { > Label+i->Caption = i; > } > > Any idea? Can I do something like that? > > Thanks and regards > > Alexandre
![]() |
0 |
![]() |
Alexandre Paulino Sierra da Silva wrote: > Hi John, thank you for your answer. > How to create this vector? is this the way? > TLabel *Labels; > Labels = new TLabel[3]; You are now creating an array of pointers, you are creating just a regular array of TLabels. Your codes attemps to create five new instances of class TLabel. However, since TLabel is a class, you need to call a constructor. Since you did not provide parentheses, (void) is assumed (defautl constructor), but TLabel does not have such a default constructor. Here is how to create an array of pointers to TLabels, containing 5 elements: TLabel *Labels[5]; This array does not contain any data or space for data. It contains five spaces, each the size of a pointer. If you would rather make a dynamic array to pointers (i.e. the number of labels is not known at compile time) TLabel **Labels; Labels = new TLabel*[5]; The first line declares what is know as a 2-dimension array, or in our case, a 1-dimension array of pointers. The second line allocate memory for the array (5 elements... the 5 could be a variable). We do not allocate the second dimension, since we will be using it as pointers to the labels. Whichever method you choose, you can than do the following: Labels[0] = Label1; Labels[1] = Label2; Labels[2] = Label3; Labels[3] = Label4; Labels[4] = Label5; I have copied the address of each of my 5 labels (created using the designer). I can than do: for(int i = 0; i < 5; i++) { Labels[i]->Caption = (AnsiString)"This is label " + (i+1); } > > I'm getting the error "[BCC32 Error] > UnitCadastroPedidoLancarCatalogo.cpp(126): E2248 Cannot find default > constructor to initialize array element of type 'TLabel'" > > Thank you. BTW I found your original question quite interesting (read funny... no offense intended). Although C is the first programming language I've learned (not considering my attempts at Basic as a kid) but then after I have learned PHP and spent a lot of time on it, I wanted to do the same in C++... Label+i->Caption = someCaption+i... just like the PHP way of doing things. However you have to remember something. PHP (or whatever other script language you got your idea from) have some way to represent data that is very free. C++, once compiled, ends up as a machine language (which is pretty incomprehensible to most of us, which is why we use a programming language in the first place). C++ gives us variables written in human language, but once compiled those variables are no more. Your "int a" will be no more. Variable 'a' will not be in the program as a textual form, it will be just a memory space which the compiler will allocate for you. This is why "int a+i" cannot create a new name for the variable as it would in PHP.
![]() |
0 |
![]() |
Tom Duhamel wrote: >If you would rather make a dynamic array to pointers (i.e. the number of >labels is not known at compile time) > > TLabel **Labels; > Labels = new TLabel*[5]; For the dynamic version, perhaps easier to use what John Cibulskis suggested: vector<TLabel*> Labels(5);
![]() |
0 |
![]() |
Alexandre Paulino Sierra da Silva wrote: > Hello! > How to make a variable receive an increment in the name? > I want to do it for example: > > I have: > Label1->Caption = "1"; > Label2->Caption = "2"; > Label3->Caption = "3"; > Label4->Caption = "4"; > > And I want to do something like that: > > for (i=1; i<5; i++) > { > Label+i->Caption = i; > } > > Any idea? Can I do something like that? In this particular case, this Label+i->Caption = static_cast<char>('0'+i); should work. If this was just an example and in reality you need anything else than single-digit numbers or if you're on a machine using an insane encoding, follow the other advice. > Thanks and regards > > Alexandre HTH, Schobi
![]() |
0 |
![]() |
Hendrik Schober <Spamtrap@gmx.de> wrote: > In this particular case, this > > Label+i->Caption = static_cast<char>('0'+i); Boggle! (You're indirecting off the loop index?) Alan Bellingham -- Team Browns ACCU Conference 2009: Wed 22 - Sat 25 April 2009, Oxford
![]() |
0 |
![]() |
Alexandre Paulino Sierra da Silva wrote: > Hello! > How to make a variable receive an increment in the name? > I want to do it for example: > > I have: > Label1->Caption = "1"; > Label2->Caption = "2"; > Label3->Caption = "3"; > Label4->Caption = "4"; > > And I want to do something like that: > > for (i=1; i<5; i++) > { > Label+i->Caption = i; > } > > Any idea? Can I do something like that? > > Thanks and regards > > Alexandre Below is a piece of code from a menu which can have upto 20 buttons. In this case the buttons are A - (upto) T, but all buttons may exist for the purpose To create a dynamic variable, you could add the line SBp[buttonCount]->Name = "Button"+IntToStr(buttonCount); //---------------------------------------------------------------------- ----- void __fastcall TFrmMain::CreateButtons() { // Read the ini file AnsiString FileName = ChangeFileExt(ParamStr(0), ".ini"); TIniFile* Ini = new TIniFile(FileName); CommPort->DeviceName=Ini->ReadString("Serial", "ComPort", "COM1"); // Buttons can be from A to T, so create the button letters char ch[2]; ch[1] = 0; buttonCount = 0; // Create the buttons based on the ini file. We may not have 20 for(int i = 0; i < MAX_BUTTON_COUNT; ++i) { ch[0] = i+'A'; AnsiString AChar = ch; if(Ini->ReadString("SwitchNames", AChar, "").Length()) { SBp[buttonCount] = new TcxButton(FrmMain); SBp[buttonCount]->Parent = FrmMain; SBp[buttonCount]->Font->Name = "Terminal"; SBp[buttonCount]->Font->Size = 10; SBp[buttonCount]->Left = BUTTON_LEFT; SBp[buttonCount]->Top = BUTTON_TOP; SBp[buttonCount]->Caption = AChar; SBp[buttonCount]->Tag = i+'A'; SBp[buttonCount]->OnClick = SpeedButtonClick; SBp[buttonCount]->Hint = "Press "+AChar+" or click this button to turn on the port"; SBp[buttonCount]->TabStop = false; SBp[buttonCount]->Caption = AnsiString("&")+AChar + " - "+Ini->ReadString("SwitchNames", AChar, ""); SBp[buttonCount]->Margin = 5; buttonCount++; } } delete Ini; // Put the captions on the buttons int length = 0; for(int i = 0; i < buttonCount; ++i) { if(length < SBp[i]->Caption.Length()) length = SBp[i]->Caption.Length(); } for(int i = 0; i < buttonCount; ++i) SBp[i]->Width = 18+length *8; ArrangeButtons(); } -- -Michael Gillen Hollister, CA UTC/GMT -8
![]() |
0 |
![]() |
Alan Bellingham wrote: > Hendrik Schober <Spamtrap@gmx.de> wrote: > >> In this particular case, this >> >> Label+i->Caption = static_cast<char>('0'+i); > > Boggle! > > (You're indirecting off the loop index?) Dang! I didn't pay any attention to that VCL stuff. I suppose that 'Caption' needs an 'AnsiString' or whatever string type this is, tight? Sorry. Brain fart. > Alan Bellingham Schobi
![]() |
0 |
![]() |
> Below is a piece of code from a menu which can have upto 20 buttons. In > this case the buttons are A - (upto) T, but all buttons may exist for > the purpose > -- > -Michael Gillen > Hollister, CA > UTC/GMT -8 Very similar to some code that I wrote. You know what they say about great minds ! Here is mine (which is in an object): //--------------------------------------------------------------------------- void TJMCStampImage::CreateButtons() { int HorOffset,VerOffset; int ButLeft,ButTop; HorOffset = 0; VerOffset = 0; if(ButtonPosition == JMC_STI_BUT_BOTTOM) { if(CenterButtons) { int ButtonsWidth; int LeftOver; ButtonsWidth = (FNmbButtons - 1)*JMC_STI_BUT_HOR_OFF + JMC_STI_BUT_WIDTH; LeftOver = PWidth - ButtonsWidth; ButLeft = PLeft + LeftOver/2; } else ButLeft = PLeft; HorOffset = JMC_STI_BUT_HOR_OFF; ButTop = PTop + ScrollBox->Height + JMC_STI_BUT_VER_DELTA; } if(ButtonPosition == JMC_STI_BUT_LEFT) { if(CenterButtons) { int ButtonsHeight; int LeftOver; ButtonsHeight = (FNmbButtons - 1)*JMC_STI_BUT_VER_OFF + JMC_STI_BUT_HEIGHT; LeftOver = PHeight - ButtonsHeight; ButTop = PTop + LeftOver/2;; } else ButTop = PTop; VerOffset = JMC_STI_BUT_VER_OFF; ButLeft = PLeft; } for(int i=0;i<FNmbButtons;i++) { Buttons[i] = new TButton(Parent); Buttons[i]->Parent = Parent; Buttons[i]->Left = ButLeft; Buttons[i]->Top = ButTop; Buttons[i]->Height = JMC_STI_BUT_HEIGHT; Buttons[i]->Width = JMC_STI_BUT_WIDTH; ButLeft += HorOffset; ButTop += VerOffset; FButtonType[i] = JMC_STI_BUT_BLANK; //Default button type Buttons[i]->Visible = false; //is blank and non-visible Buttons[i]->Tag = i; } } //--------------------------------------------------------------------------- John
![]() |
0 |
![]() |
Hendrik Schober <Spamtrap@gmx.de> wrote: > Dang! I didn't pay any attention to that VCL stuff. It was the VCL stuff (how to actually get Label1, then Label2, etc. in a loop) that the user appeared to be asking about. Alan Bellingham -- Team Browns ACCU Conference 2009: Wed 22 - Sat 25 April 2009, Oxford
![]() |
0 |
![]() |
Alan Bellingham wrote: > Hendrik Schober <Spamtrap@gmx.de> wrote: > >> Dang! I didn't pay any attention to that VCL stuff. > > It was the VCL stuff (how to actually get Label1, then Label2, etc. in a > loop) that the user appeared to be asking about. "How to make a variable receive an increment in the name?" Not having used VCL for about a decade I got into the habit to not to see references to VCL in postings that deal with non-VCL stuff, too. Of course, that doesn't excuse that I didn't even have a look at what's left of the equal sign. Mea culpa. (I often post while waiting for the compiler, and sometimes I don't pay enough attention...) > Alan Bellingham Schobi
![]() |
0 |
![]() |
How about just: Labels[i]->Caption = IntToStr(i); John > Alan Bellingham wrote: >> Hendrik Schober <Spamtrap@gmx.de> wrote: >> >>> In this particular case, this >>> >>> Label+i->Caption = static_cast<char>('0'+i); >>
![]() |
0 |
![]() |
Is this always going to be for visual controls, and will you want to do it for all controls of a certain type on a certain parent (eg, on the form, or on a panel, etc)? If so, here's a different approach: you could iterate through the Controls (I think? Haven't got the help in front of me) property, something along the lines of: int iLabelsSeen = 0; for (int i = 0; i < Form1.ControlCount; ++i) { TLabel* pLabel = dynamic_cast<TLabel*>(Form1.Controls[i]); if (pLabel != NULL) { pLabel->Caption := IntToStr(++iLabelsSeen); } } Untested code, and I'm rather tired today - blame all bugs on that :) The Controls property of a TWinControl holds all controls with that TWinControl as their parent. Cheers, David > {quote:title=Alexandre Paulino Sierra da Silva wrote:}{quote} > Hello! > How to make a variable receive an increment in the name? > I want to do it for example: > > I have: > Label1->Caption = "1"; > Label2->Caption = "2"; > Label3->Caption = "3"; > Label4->Caption = "4"; > > And I want to do something like that: > > for (i=1; i<5; i++) > { > Label+i->Caption = i; > } > > Any idea? Can I do something like that? > > Thanks and regards > > Alexandre
![]() |
0 |
![]() |
Hi Alexandre, I do this (similar to David M., above?) //--------------------------------------------------------------------------- void TEditForm::InitializeControlPanelForm( void) { // initialize control panel... int count; int height; int left; int top; int width; const int border_size = 2; const int button_size = 108; left = border_size; top = border_size; height = 24; width = button_size; count = 1; ControlForm->Top = EditForm->Top; for( int index = 0, cindex = 1; control_buttons_text[index].Length() != 0; ++index) { TButton *button = AddControlPanelButton( control_buttons_text[index]); button->Height = height; button->Left = left; button->Top = top; button->Width = width; button->Font->Name = "Arial"; button->Font->Size = 7; button->Tag = count; left = left + width; if(cindex == 4) { top = top + height; left = border_size; cindex = 0; } cindex++; count++; } ControlForm->CloseButton->Top = top + 30; ControlForm->Label1->Top = top + 36; ControlForm->Height = top + 96; ControlForm->Width = ((width * 4) + 8); ControlForm->Left = Screen->Width - ControlForm->Width; } //--------------------------------------------------------------------------- TButton *TEditForm::AddControlPanelButton( const AnsiString &name) { // add control panel button... TButton *button = new TButton (this); button->Parent = ControlForm; button->Caption = name; // NOTE (1) Calls routine listed bellow... button->OnClick = ControlForm->Button1Click; return button; } //--------------------------------------------------------------------------- void __fastcall TControlForm::Button1Click(TObject *Sender) { TButton *button = dynamic_cast <TButton *>(Sender); AnsiString tag((int)button->Tag); selected_button = button; switch(button->Tag) { case 1: // Do stuff... break; case 2: // ad nauseare... break; case 3: // ad infinitas... break; } } //--------------------------------------------------------------------------- Bruce
![]() |
0 |
![]() |