addressing sequentially named objects - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

addressing sequentially named objects

ginrginr Posts: 1Member
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

Comments

  • luckyboyluckyboy Posts: 105Member
    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
    [code]
    OnClick()
    {
    for(int i=0; iControlCount; i++)
    {
    TLabel* activeControl = (TLabel*)GroupBox1->Controls[i];
    activeControl->Caption = IntToStr(i);
    }
    }
    [/code]

    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
  • PiSymbolPiSymbol Posts: 13Member
    : 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
    : [code]:
    : OnClick()
    : {
    : for(int i=0; iControlCount; i++)
    : {
    : TLabel* activeControl = (TLabel*)GroupBox1->Controls[i];
    : activeControl->Caption = IntToStr(i);
    : }
    : }
    : [/code]:
    :
    : 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'.
    [code]OnClick()
    {
    // count all controls
    for(int i=0; iControlCount; 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);
    }
    }
    }[/code]

    Good luck,

    PiSymbol


Sign In or Register to comment.