locked
Example iterating through a Controls Collection using for each in C++/CLI RRS feed

  • Question

  • Hello All,

     

    I am trying to use the for each on a Controls collection as I had in C# but I can't seem to get it working.

     

    Any help would be appreciated Smile

     

    Bob Hanson

    Friday, August 3, 2007 7:35 PM

Answers

  •  BobH wrote:

    Hello All,

     

    I am trying to use the for each on a Controls collection as I had in C# but I can't seem to get it working.

     

    Any help would be appreciated

     

    Bob Hanson

    Hi Bob,

     

    As crescens2k indicated, provide your existing code is more appropriate way to help you out. Since you are mentioned examples of iterating through a control collection, here is a simple one that works for me:

    Create a winform application use wizard, add a button and some other controls including one or more text box, then in the button click event handler, add following code to iterate through the controls in control collection:

    Code Snippet

    private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
         for each(Control ^pC in this->Controls){
          if(pC->GetType() == System::Windows::Forms::TextBox::typeid)
          pC->Text = "whatever";
         }
        }

     

     

    hope it helps

    rico

    Wednesday, August 8, 2007 4:19 AM

All replies

  • Well, could you post the code you have since that would be the best way to help you out.

    Friday, August 3, 2007 7:46 PM
  •  BobH wrote:

    Hello All,

     

    I am trying to use the for each on a Controls collection as I had in C# but I can't seem to get it working.

     

    Any help would be appreciated

     

    Bob Hanson

    Hi Bob,

     

    As crescens2k indicated, provide your existing code is more appropriate way to help you out. Since you are mentioned examples of iterating through a control collection, here is a simple one that works for me:

    Create a winform application use wizard, add a button and some other controls including one or more text box, then in the button click event handler, add following code to iterate through the controls in control collection:

    Code Snippet

    private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
         for each(Control ^pC in this->Controls){
          if(pC->GetType() == System::Windows::Forms::TextBox::typeid)
          pC->Text = "whatever";
         }
        }

     

     

    hope it helps

    rico

    Wednesday, August 8, 2007 4:19 AM