none
Is there a way to loop through dynamically created controls?

    Question

  • After doing some research, I'm trying to loop through some CheckBoxes I add dynamically to see if they are checked and their name contains "dfm". This code, though, is as close as I've come and it fails:

        static public void EnumVisual( Visual myVisual )
        {
          List<String> listCheckedDFMFiles = new List<String>();
         
          for (int i = 0; i < VisualTreeHelper.GetChildrenCount(myVisual); i++)
          {
            // Retrieve child visual at specified index value.
            Visual childVisual = (Visual)VisualTreeHelper.GetChild(myVisual, i);

            // Do processing of the child visual object.
            if ((childVisual is CheckBox) &
               ((CheckBox)childVisual.IsChecked.HasValue) &&
               ((CheckBox)childVisual.IsChecked.Value == true) &&
               ((CheckBox)childVisual.Name.ToString().Contains("dfm")))
            {
              listCheckedDFMFiles.Add((CheckBox)childVisual.Name);
            }

            // Enumerate children of the child visual object.
            EnumVisual(childVisual);
          }
        }

    The name of the Window/form on which these controls reside is named WindowConvert, and so I call it this way:

    EnumVisual(WindowConvert);

    Does anybody know how to get this to work?


    Delphi ancient/C# newbie
    Thursday, June 18, 2009 4:31 PM

Answers

  • Hi,

     

    In your previous code, every time you invoke the EnumVisual method recursively you create a completely new listCheckedDFMFiles object, meaning that the CheckBoxs meets your requirement are not all added in the collection as you expected. So you need to make the listCheckedDFMFiles as a global variable like the following code, so that you can get all the CheckBox as you want.

    Code snippet:

     

       List<String> listCheckedDFMFiles;

            public void EnumVisual(Visual myVisual)

            {

                listCheckedDFMFiles = new List<String>();

                for (int i = 0; i < VisualTreeHelper.GetChildrenCount(myVisual); i++)

                {

                    // Retrieve child visual at specified index value.

                    Visual childVisual = (Visual)VisualTreeHelper.GetChild(myVisual, i);

                    // Do processing of the child visual object and add it to the list collection

     

                    // Enumerate children of the child visual object.

                    EnumVisual(childVisual);

                }

            }

     

    Hope this helps.

    Thanks


    Jim Zhou -MSFT
    Thursday, June 25, 2009 9:13 AM

All replies

  • Please check the following article...

    http://www.codeproject.com/kb/wpf/witiymiwyg.aspx

    http://www.codeproject.com/kb/wpf/colorcube.aspx

    I hope this will help you :)

    Prasad - www.beautifulmind.blog.co.in
    Microsoft MVP
    Thursday, June 18, 2009 4:54 PM
  • Hi,

     

    In your previous code, every time you invoke the EnumVisual method recursively you create a completely new listCheckedDFMFiles object, meaning that the CheckBoxs meets your requirement are not all added in the collection as you expected. So you need to make the listCheckedDFMFiles as a global variable like the following code, so that you can get all the CheckBox as you want.

    Code snippet:

     

       List<String> listCheckedDFMFiles;

            public void EnumVisual(Visual myVisual)

            {

                listCheckedDFMFiles = new List<String>();

                for (int i = 0; i < VisualTreeHelper.GetChildrenCount(myVisual); i++)

                {

                    // Retrieve child visual at specified index value.

                    Visual childVisual = (Visual)VisualTreeHelper.GetChild(myVisual, i);

                    // Do processing of the child visual object and add it to the list collection

     

                    // Enumerate children of the child visual object.

                    EnumVisual(childVisual);

                }

            }

     

    Hope this helps.

    Thanks


    Jim Zhou -MSFT
    Thursday, June 25, 2009 9:13 AM
  • Great !
    Monday, July 13, 2009 12:15 AM