Solved

Counting checks (BCB5)

Posted on 2002-03-07
4
175 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
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.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

920 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now