Return of: Help With Recursive Function

I've asked this question recently and had some great help.  Now I'd like to modify it and I can't figure out the next step.  My new function would return an arraylist of the textbox objects I'm recursively searching for.  Here's the current function which just returns a boolean value if textboxes are found.

private bool ContainsWritableTextBoxes(
     Control ContainerToSearch)
   {
     bool TextBoxFound = false;

     if ( ContainerToSearch.HasChildren )
     {
       foreach ( Control child in ContainerToSearch.Controls )
       {
         if ( child is TextBox && !((TextBox)child).ReadOnly )
         {
             TextBoxFound = true;
             break;
         }
         else if ( child.HasChildren && child is Panel && 
            child.Visible && ContainsWritableTextBoxes( child ) )
         {
           TextBoxFound = true;
           break;
         }
       }
     }

     return TextBoxFound;
   }
LVL 1
beaudetiousAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

smeggheadCommented:
Just declare your arraylist outside of the procedure, at class scope, then, when you find a textbox, add it to the list.

ArrayList MyAL;

then, before calling the 'ContainsWritableTextBoxes' function, set the MyAL=new ArrayList();

then, from your code..

MyAL.Add(child);
beaudetiousAuthor Commented:
smegghead,

could you copy my code and place your change in it.  I'm having a little problem visualizing what you are talking about.  I was thinking about this today a bit and was wondering if I passed in the array list by ref and used it as follows:

private void ContainsWritableTextBoxes(
    Control ContainerToSearch,
    ref ArrayList MyAL)
  {
    bool TextBoxFound = false;

    if ( ContainerToSearch.HasChildren )
    {
      foreach ( Control child in ContainerToSearch.Controls )
      {
        if ( child is TextBox && !((TextBox)child).ReadOnly )
        {
            MyAl.Add( child );
        }
        else if ( child.HasChildren && child is Panel && 
            child.Visible)
        {
          ContainsWritableTextBoxes( child, MyAL );
        }
      }
    }  
  }

or something like that.  Would that work?
smeggheadCommented:
Yes you can do it that way.. the way I was thinking of was...

public class MyClass
private ArrayList MyAL;
public void Main()
{
        MyAL=new ArrayList(0);
        ContainsWritableTextBoxes(Whatever);
}

private bool ContainsWritableTextBoxes(
    Control ContainerToSearch)
  {
    bool TextBoxFound = false;

    if ( ContainerToSearch.HasChildren )
    {
      foreach ( Control child in ContainerToSearch.Controls )
      {
        if ( child is TextBox && !((TextBox)child).ReadOnly )
        {
            TextBoxFound = true;
            MyAL.Add( child );
            break;
        }
        else if ( child.HasChildren && child is Panel && 
            child.Visible && ContainsWritableTextBoxes( child ) )
        {
          TextBoxFound = true;
          break;
        }
      }
    }

    return TextBoxFound;
  }


Incidentally, two other points..

why are you calling a break command after finding the first textbox.. surely if you want a list of all of them, you should continue looping.

your method is equally correct, although there is no need to pass the ArrayList with 'ref'.  General parameter passing (without 'ref') allows you to modify the contents of a passed class, just not the actual class, so you couldn't set it = to 'new ArrayList()' within the procedure.

HTH
Smg.
Become a CompTIA Certified Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

beaudetiousAuthor Commented:
smegghead,

I'm still confusing myself.  In the original flavor of my code I was returning true/false in order to set the state of another control.  Hence, the breaks in the logic.  If I found at least 1 textbox with read-only set to false then I return true and I'm happy.  Now, I want to grab a reference to every textbox with read-only set to false in the visible panels of the originally passes in container.  

So I won't need the breakpoints and I can change to a return type of void instead of boolean.  However, is this the correct way to recursively call the function if the current child is not a textbox and it passes the else if logic:

       else if ( child.HasChildren && child is Panel && 
            child.Visible)
       {
         ContainsWritableTextBoxes( child, MyAL );
       }


or should it be

       else if ( child.HasChildren && child is Panel && 
            child.Visible)
       {
         ContainsWritableTextBoxes( child, ref MyAL );
       }


I'm making you work hard for the points ain't I?
smeggheadCommented:
both examples will work, but, as I explained b4, there is no need to pass MyAL using 'ref'. Try it both ways.. you should get the same results.

I'm a hard worker anyway :-)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
beaudetiousAuthor Commented:
I used my code but just being able to discuss it with someone helps.  It works great.  Thanks.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.