Solved

Counting checks (BCB5)

Posted on 2002-03-07
4
173 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

772 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

14 Experts available now in Live!

Get 1:1 Help Now