Solved

Counting checks (BCB5)

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Suggested Solutions

Title # Comments Views Activity
is twain_32.dll cmpatible with windows 10 ? 10 237
C++ question 3 74
Arduino EDI - Programming language 3 117
show out valin of json for debugging in visaul c++ 1 48
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…
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 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.

726 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