Solved

Counting checks (BCB5)

Posted on 2002-03-07
4
176 Views
Last Modified: 2010-05-18
Greetings,

What is the most efficient way to count the number of TCheckbox components on on a form or frame which are checked (or unchecked as the case may be).

I could do it by looping through all the controls and then keeping a counter but this just seems to be overkill.

Thanks,

Robert Lancaster
0
Comment
Question by:zaroblan
  • 3
4 Comments
 
LVL 1

Expert Comment

by:wolfpackinc
ID: 6848148
in the onclick events of the TCheckBox enter:

void __fastcall TMainForm::CheckBox1Click(TObject *Sender)
{
if(CheckBox1->Checked)
{
CheckBoxesChecked=CheckBoxesChecked+1;
}
else
{
CheckBoxesChecked=CheckBoxesChecked-1;
}
Panel1->Caption = CheckBoxesChecked;
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::CheckBox2Click(TObject *Sender)
{
if(CheckBox2->Checked)
{
CheckBoxesChecked=CheckBoxesChecked+1;
}
else
{
CheckBoxesChecked=CheckBoxesChecked-1;
}
Panel1->Caption = CheckBoxesChecked;
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::CheckBox3Click(TObject *Sender)
{
if(CheckBox3->Checked)
{
CheckBoxesChecked=CheckBoxesChecked+1;
}
else
{
CheckBoxesChecked=CheckBoxesChecked-1;
}
Panel1->Caption = CheckBoxesChecked;
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::CheckBox4Click(TObject *Sender)
{
if(CheckBox4->Checked)
{
CheckBoxesChecked=CheckBoxesChecked+1;
}
else
{
CheckBoxesChecked=CheckBoxesChecked-1;
}
Panel1->Caption = CheckBoxesChecked;
}


in the private section of your header file:

private:     // User declarations
int CheckBoxesChecked; //global variable
0
 
LVL 1

Expert Comment

by:wolfpackinc
ID: 6848173
Better yet do this in your first CheckBox:

void __fastcall TMainForm::CheckBox1Click(TObject *Sender)
{
if (Sender->ClassNameIs("TCheckBox"))
{
TCheckBox *pTV = (TCheckBox *)Sender;


if(pTV->Checked)
{
CheckBoxesChecked=CheckBoxesChecked+1;
}
else
{
CheckBoxesChecked=CheckBoxesChecked-1;
}
Panel1->Caption = CheckBoxesChecked;
}
}

and then set all other onclick events of the other CheckBoxes to CheckBox1Click()
0
 
LVL 1

Accepted Solution

by:
wolfpackinc earned 50 total points
ID: 6910567
void __fastcall TMainForm::CheckBox1Click(TObject *Sender)
{
if (Sender->ClassNameIs("TCheckBox"))
{
TCheckBox *pTV = (TCheckBox *)Sender;


if(pTV->Checked)
{
CheckBoxesChecked=CheckBoxesChecked+1;
}
else
{
CheckBoxesChecked=CheckBoxesChecked-1;
}
Panel1->Caption = CheckBoxesChecked;
}
}

and then set all other onclick events of the other CheckBoxes to CheckBox1Click()
0
 

Author Comment

by:zaroblan
ID: 6912163
Great !!! This works. I, however, came up with a more generic approach. I wrote a class to handle it. You pass the class a container (a form, group box or otherwise) and then process them in that fashion. It also provides a vehicle for getting and setting all of them with one method call.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question