vendredi 2 décembre 2016

Find dynamically created CheckBoxes (C++ Windows Form Application)

by runtime I create CheckBoxes:

unsigned int number = getNumber();
CheckBox^ box;

for(unsigned int i = 1; i <= number; i++)
{
    box = gcnew CheckBox();
    box->Name = i.ToString();
    box->Text = i.ToString();
    box->AutoSize = true;
    box->Location = Point(10, i * 30);
    this->panel->Controls->Add(box);      
}

Now I want to get all CheckBoxes, that are NOT checked:

std::map<unsigned int, std::string> values;

for (unsigned int i = 1; i <= number; i++)
{
    String^ name = i.ToString();
    CheckBox^ box = dynamic_cast<CheckBox^>(this->panel->Controls->Find(name, true));
    if (!box->Checked)
    {
        String^ text = box->Text;
        values.insert(std::pair<unsigned int, std::string>(i, msclr::interop::marshal_as<std::string>(text)));
    }
}

My problem is, that by runtime I get a NullReferenceException (in the line I check if the box is unchecked). But all the CheckBoxes exist.

Can anybody help me?

Thanks a lot!

Greetings Train.

PS: I am using Visual Studio 2015 Community Update 3




Aucun commentaire:

Enregistrer un commentaire