Referencing controls in list
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Referencing controls in list

  1. #1
    Join Date
    Oct 2013
    Posts
    2

    Referencing controls in list

    Hello,
    I created a custom control (ErrorButton). When the main form loads i add some ErrorButtons to the controls collection and inside a list.
    Code:
     
    List<ErrorButton> errorButtonList = new List<ErrorButton>();
    while (...)
                    {
                        ErrorButton eb = new ErrorButton();
                        // doing stuff
                        this.Controls.Add(eb);
                        errorButtonList.Add(eb);
                    }
    But i don't want to reset them like this.
    Code:
                foreach (Control c in this.Controls)
                {
                    if (c is ErrorButton)
                    {
                        ErrorButton er = (ErrorButton)c;
                        er.Reset();
                    }
                }
    I want to reset them like this.
    Code:
    foreach (ErrorButton er in errorButtonList)
                {
                    er.Reset();
                }
    But it doesn't work. I thought the ErrorButtons are referenced inside the list, so any changes will change the ErrorButtons inside the form collection.

  2. #2
    Join Date
    Oct 2013
    Posts
    16

    Re: Referencing controls in list

    Try in this way
    Code:
    foreach (ErrorButton er in this.Controls.OfType<ErrorButton>())
                {
                    er.Reset();
                }

  3. #3
    Join Date
    Oct 2013
    Posts
    2

    Re: Referencing controls in list

    Yes it works, thanks Reroto.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center