4
votes

I want to check all checkboxes when click on the button. All object are in form application of visual studio 2010 c++. The point of problem is that every object (checkbox) has various name, CheckBox1, CheckBox2, ... I make UnicodeString with value "CheckBox", and int value that begin with 1, and put it together in third variable to find object, and that's work, but don't have a clue how to check all those boxes, please help.

Windows 7, 64, Visual studio 2010(c++) or c++ builder 2010

3
In Win32 API proper, checkboxes don't have names - they have numeric IDs. I think you're using C++ Builder's library (is it still called OWL?) and mistaking it for WinAPI. Retag please. - Seva Alekseyev
Which is it? C++ Builder or Visual Studio 2010? - Cody Gray
They're not the same. I don't know everything I should about C++ Builder, but I understand it provides a custom wrapper library around the Win32 API. If you're using that library, the code you write targeting it is not going to transfer to Visual C++. - Cody Gray

3 Answers

2
votes

I did something similar for another component, this is how I did using C++ Builder.

for (int i = 0; i < this->ComponentCount; i++)
{
 TCheckBox *TempCheckBox = dynamic_cast<TCheckBox *>(this->Components[i]);
 if (TempCheckBox)
 {
   TempCheckBox->Checked = true;
 }
}

This will iterate through all the components on your form, if the component is a TCheckBox it will be checked.

1
votes

Why dont you add everything to a vector containing checkboxes, and then iterate through them all when necessary? This will allow you to reference each checkbox individually, but yet all at once.

     cliext::vector<System::Windows::Forms::CheckBox^> items;
     items.push_back(checkbox1);
     .
     .
     .
     items.push_back(checkboxN);

It is important that you also include

#include <cliext/vector>

due to the fact that the normal vector in the standard library is currently unable to support this control.

1
votes

In C++Builder, you can place all of your TCheckBox* pointers into an array or std::vector, which you can then loop through when needed, eg:

TCheckBox* cb[10];

__fastcall TForm1::TForm1(TComponent *Owner)
    : TForm(Owner)
{
    cb[0] = CheckBox1;
    cb[1] = CheckBox2;
    ...
    cb[9] = CheckBox10;
}

void __fastcall TForm1::Button1Click(TObject *Sender)
{
    for (int i = 0; i < 10; ++i)
        cb[i]->Checked = true;
}

If you have a lot of checkboxes and do not want to fill in the entire array by hand, you can use a loop instead:

__fastcall TForm1::TForm1(TComponent *Owner)
    : TForm(Owner)
{
    for (int i = 0; i < 10; ++i)
        cb[i] = (TCheckBox*) FindComponent("CheckBox" + IntToStr(i+1));
}