0
通过运行时创建复选框:查找动态创建的CheckBox(C++ Windows窗体应用程序)
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);
}
现在,我想所有的复选框未签:
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)));
}
}
我的问题是,通过运行时我得到一个NullReferenceException(在我检查框是否未选中)。 但是所有的复选框都存在。
PS:我使用Visual Studio 2015年社区更新3
为什么你使用^?你是否期望像Bitwise异或赋值? – Rama