C++ Builder

Moderators: Lundin
Number of threads: 509
Number of posts: 1146

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
addressing sequentially named objects Posted by ginr on 4 Mar 2008 at 12:45 AM
Hello

I am currently working in CBuilder 4, but I suppose the following question/concept will apply to any version.

When dropping a new object onto a form, (eg. a label) CBuilder automatically names it, with the object type and a number, (eg. label1)
When more objects are dropped, the object numbers are sequentially incremented (label2, label3, label4, etc)

Now, if I wanted to modify a common field in a whole bunch of objects (say label1 to label10) using a loop, how would I address these objects?

-g
Report
Re: addressing sequentially named objects Posted by luckyboy on 7 Mar 2008 at 7:23 PM
I'm not sure i understood the question
any way if you want to loop through controls
you have to:
1- make sure your interested controls are in same container (say groupBox)
2- make sure their tab stops are sequenced
3- use the following code
OnClick()
{    
    for(int i=0; i<GroupBox1->ControlCount; i++)
    {
        TLabel* activeControl = (TLabel*)GroupBox1->Controls[i];
        activeControl->Caption = IntToStr(i);
    }
}


note1: if Tab Stops are not in sequence, try to jump with i in the loop
note2: remember that Form is a container itself, so you can use Controls[index] without GorupBox preceeding it, but this will affect all controls on the form.

Mohammad Nasim
Report
Re: addressing sequentially named objects Posted by PiSymbol on 8 Mar 2008 at 3:45 AM
: I'm not sure i understood the question
: any way if you want to loop through controls
: you have to:
: 1- make sure your interested controls are in same container (say
: groupBox)
: 2- make sure their tab stops are sequenced
: 3- use the following code
:
: 
: OnClick()
: {    
:     for(int i=0; i<GroupBox1->ControlCount; i++)
:     {
:         TLabel* activeControl = (TLabel*)GroupBox1->Controls[i];
:         activeControl->Caption = IntToStr(i);
:     }
: }
: 
:
:
: note1: if Tab Stops are not in sequence, try to jump with i in the
: loop
: note2: remember that Form is a container itself, so you can use
: Controls[index] without GorupBox preceeding it, but this will affect
: all controls on the form.
:
: Mohammad Nasim

I think it's more save to check if the TControl which you'd like to change is really a 'TLabel'. You should use 'InheritsFrom'.
OnClick()
{
  // count all controls
  for(int i=0; i<GroupBox1->ControlCount; i++)
	{
	  // check if the control is really a TLabel
	  if(GroupBox1->Controls[i]->InheritsFrom(__classid(TLabel)))
		{
		  // change the name of the label starting with 1
		  ((TLabel *)GroupBox1->Controls[i])->Name = "Label" + IntToStr(i + 1);
		}
	}
}


Good luck,

PiSymbol





 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.