How to check all checkboxes in a form application programmatically?

I want to check all the checkboxes when I click on the button. All objects are in the form of a visual studio 2010 C ++ application. The fact is that each object (check box) has a different name, CheckBox1, CheckBox2, ... I make a UnicodeString with the value "CheckBox" and the int value that starts with 1, and combines it in a third variable to search for the object, and this works, but has no idea how to check all of these fields, please help.

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

+4
source share
3 answers

I did something similar for another component, so I used 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 cause all components in your form to repeat, if the component is TCheckBox, it will be checked.

+2
source

Why don't you add everything to the vector containing the checkboxes and then iterate over them all when necessary? This will allow you to link to each flag separately, but all at once.

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

It’s important that you also enable

 #include <cliext/vector> 

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

+1
source

In C ++ Builder, you can put all your TCheckBox* pointers in an array or std::vector , which can then be executed if necessary, for example:

 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 many checkboxes and you do not want to fill the entire array manually, 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)); } 
+1
source

Source: https://habr.com/ru/post/1386627/


All Articles