/*_________________________________________________________________ * * F O R E A C H L O O P D E M O * *_________________________________________________________________ * * */ //Updated 5/11/2016 Removed group box and made project simple. //This program shows you how to use a foreach loop to clear any number of textboxes //on a form. #pragma once namespace My8ForEachLoops { using namespace System; using namespace System::ComponentModel; using namespace System::Collections; using namespace System::Windows::Forms; using namespace System::Data; using namespace System::Drawing; /// /// Summary for Form1 /// public ref class Form1 : public System::Windows::Forms::Form { public: Form1(void) { InitializeComponent(); // //TODO: Add the constructor code here // } protected: /// /// Clean up any resources being used. /// ~Form1() { if (components) { delete components; } } protected: //our controls collection internal: System::Windows::Forms::Button^ btnEnd; private: internal: System::Windows::Forms::TextBox^ TextBox3; internal: System::Windows::Forms::TextBox^ TextBox2; internal: System::Windows::Forms::TextBox^ TextBox1; internal: System::Windows::Forms::Button^ btnClear; private: System::Windows::Forms::RadioButton^ radioButton1; internal: private: System::Windows::Forms::RadioButton^ radioButton2; private: System::Windows::Forms::RadioButton^ radioButton3; private: System::Windows::Forms::CheckBox^ checkBox1; private: System::Windows::Forms::CheckBox^ checkBox2; private: System::Windows::Forms::CheckBox^ checkBox3; private: /// /// Required designer variable. /// System::ComponentModel::Container ^components; //***********START OF MY CODE************* private: System::Void btnClear_Click(System::Object^ sender, System::EventArgs^ e) { for each (Control^ myControl in this->Controls) //loop through the form's control collection to find all the textboxes. { if (myControl->GetType() == System::Windows::Forms::TextBox::typeid) //found textbox? then clear text myControl->Text = ""; } TextBox1->Focus(); } }; }