?
Solved

Counting checks (BCB5)

Posted on 2002-03-07
4
Medium Priority
?
180 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 150 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
Suggested Courses

770 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