托管代码中遍历问题
for each(Control^ mCtrl in this->Controls)
{
if (mCtrl->GetType() == compareGroupBox->GetType())
{
plane1->Controls->Add((System::Windows::Forms::GroupBox^)mCtrl);
((System::Windows::Forms::GroupBox^)mCtrl)->BringToFront();
((System::Windows::Forms::GroupBox^)mCtrl)->Dock = DockStyle::Fill;
((System::Windows::Forms::GroupBox^)mCtrl)->Visible = false;
}
}
当有多个groupBox时,if内部只会执行一次.
System::Collections::ArrayList^ list = gcnew System::Collections::ArrayList;
for each(Control^ mCtrl in this->Controls)
{
if (mCtrl->GetType() == compareGroupBox->GetType())
{
list->Add((System::Windows::Forms::GroupBox^)mCtrl);
}
}
for (int i = 0;i < list->Count;++i)
{
plane1->Controls->Add((System::Windows::Forms::GroupBox^)list[i]);
((System::Windows::Forms::GroupBox^)list[i])->BringToFront();
((System::Windows::Forms::GroupBox^)list[i])->Dock = DockStyle::Fill;
((System::Windows::Forms::GroupBox^)list[i])->Visible = false;
}
list->Clear();
这种就可以。
--------------------编程问答-------------------- plane1->Controls->Add,这个会导致界面的contols截获发生变化,遍历会丢数据的,你可以倒着遍历
for(int i=xxx,i>=9;i--),就好像删除列表中元素一样,要倒着来,或者用第二种方法,先获得列表,这个肯定没问题 --------------------编程问答-------------------- 主要是这句影响的
plane1->Controls->Add((System::Windows::Forms::GroupBox^)mCtrl);
它把mCtrl移到plane1上了
补充:.NET技术 , .NET Framework