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

Checkbox problems!

Status
Not open for further replies.

RaveT

Programmer
Jun 21, 2003
4
SE
Im making a program which in the main form has about 15 checkboxes. 14 of them are different choices and the last one is for "include all".

Now to the problem.

When I checks the "Include all" button i want all the others checkboxes greyed out. But for now the code look like this:

check_1->Enabled = false;
check_2->Enabled = false;
check_3->Enabled = false;
check_4->Enabled = false;
check_5->Enabled = false;
.
.
.
.
.
.
....and so on. It seems a bit clumpsy....

But cant i make a for-loop or something instead??

for ( int a=1;a<15;a++)

but how should write the names then of the checkboxes??


Or is there another better way to do it ??
 
Something like this:

for (int x=0; x < Form1->ComponentCount; x++)
{
TComponent *Comp = Form1->Components[x];
if (Comp->Name.SubString(&quot;check_&quot;) > 0)
{
TCheckBox *Box = (TCheckBox*) Comp;
Box->Enabled = false;
}
}

Replace Form1 with whatever parent owns the components. Don't name the &quot;check all&quot; check box with check_ of course. :)

Chris
 
if (Comp->Name.SubString(&quot;check_&quot;) > 0)


sends me an error message, Substring needs (int, int)

What to do??
 
Sorry I used the wrong function. Replace SubString with AnsiPos

Chris
 
Use RTTI information instead. Faster & nice.

register TCheckBox * cb;
for (register i=0; i < Form1->ComponentCount; i++)
{
if ((cb=dynamic_cast<TCheckBox *>(Form1->Components))!=NULL)
{
cb->Enabled=false;
}
}
 
Won't that also disable the checkbox that you use to say &quot;Include All&quot; and any other checkboxes you have on the same form?

Chris
 
Yes, but U can use Panels, GroupBoxes or the common Tag property to identify your CheckBox groups.

Unborn
 
maybe not so pretty

// at the Constructor or on show or whenever

Tlist *CBox;
CBox = new TList;

CBox->Add (CheckBox1);
CBox->Add (CheckBox2);
CBox->Add (CheckBox3);
CBox->Add (CheckBox4);
CBox->Add (CheckBox5);

then at any time after the list is populated you can just go through the list with a for loop and do whatever you want.

for (int x = 0; x < CBox->Count; x++)
{
if (CBox->Items [x])
{
TCheckBox *T;
T = (CheckBox*) CBox->Items [x];
T->Checked = false;
}
}

&quot;but how should write the names then of the checkboxes??&quot;

what specifically do you mean by this.

tomcruz.net
 
Hej guys,

the last reply has a VERY IMPORTANT thing to notice:

when You create the checkboxes, the are added backwards when You set the ->Align to alTop.

The loop works, but the visualization is
last added = top checkbox.

I have done similair with checkklistboxes in a scrollbox:

The topwanted checklistbox was created last.

I have done also a nasty workaround:

While creating You could set the names of the checkboxes:
(Do this before showing the specific form)

Code:
[i]
(for int i=1; i<15; i++) 
{   
      TCheckBox *NewCB = TCheckBox(this);
      NewCB-> Parent = WhateverVisibleParent;  (THIS IS IMPORTANT!!!)
 //set all desired properties here
//and:
     NewCB->Name = &quot;checkb_&quot;+(AnsiString)i;
    
//  To remember these names / or their Values You could write these names into a 
// (permanent) Memo/into a file, where You could recall them from:

        PermanentMemo->Lines->Values[NewCB->Name] = &quot;yes&quot;;       // or &quot;1&quot; or whatever AnsiString You like  
// this line creates with i = 2 a line in the PermanentMemo:  checkb_2=yes

}
// create the &quot;all&quot; checkbox solely!


//to disable:   ( as Supernat has shown already)
AnsiString CheckBoxName; 
(for int ishow=0; ishow<  MyFormWhereWhateverVisibleParentResides->Component->Count; ishow++) 
{   
     CheckBoxName = Components[ishow]->Name;
     if ( CheckBoxName.Pos( &quot;checkb_&quot;) > 0)
     {
          (TCheckBox *)Components[ishow]->Enabled = false;     
     } 
}

[/i]

Maybe You get an idea of what is possible and this helps as well as the other posts!

Does anybody think that is too much memory intensive?
regards,

Michael
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top