Click here to Skip to main content
15,915,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm make loop to rest some controls inside form ... all work correctly expect combox coz i need to handle case ...

ClearControlsItems take arraylist from comboxs ...what i need if current combox like the combox in array ... I will display "Choose an Item" else i will empty datasource ...

But result not match what i want ?

C#
private void ClearControlsItems(Control c, ArrayList comboxNames)
       {
           foreach (Control control in c.Controls)
           {
               if (control is TextBox)
               {
                   control.Text = "";
               }
               else if (control is ComboBox)
               {
                   ComboBox combox = control as ComboBox;
                   int arrayLength = comboxNames.Count;
                   for (int i = 0; i < arrayLength; i++)
                   {
                       if (combox.Name == ((ComboBox)comboxNames[i]).Name)
                       {
                           ((ComboBox)comboxNames[i]).Text = "Choose an Item";
                           ((ComboBox)comboxNames[i]).Items.Remove(comboxNames[i]);
                           arrayLength = comboxNames.Count-1;
                       }
                       else
                       {

                           ((ComboBox)comboxNames[i]).DataSource = null;
                       }
                   }
               }

               else
               {
                   if (control.Controls.Count > 0)
                   {
                       ClearControlsItems(control, comboxNames);
                   }

               }

           }
       }
Posted
Comments
[no name] 30-Nov-11 20:26pm    
have you fixed this already? what's the error you are receiving?
Honeyboy_20 1-Dec-11 4:42am    
First thanks to Ahmed for his solution and I get the solution with sample from his blog http://ahmedbohoty.blogspot.com/2011/11/clear-all-combox-controls-expect-those.html

I would do it like this:

C#
private void ClearControls(Control parent)
{
    foreach (Control ctrl in parent.Controls)
    {
        if (ctrl.IsContainer)
        {
            ClearControls(ctrl);
        }
        else
        {
            if (ctrl is TextBox)
            {
                ((TextBox)ctrl).Text = "";
            }
            else if (ctrl is ComboBox)
            {
                // repeatedly casting is ineffecient. Cast it once.
                ComboBox combo = ctrl as ComboBox;
                switch (combo.Name)
                {
                    case "combo1" :
                    case "combo2" :
                        // do something in the combobox - it's not clear what 
                        // you're trying to achieve, maybe selecting the first
                        // item in the Items collection for the current combobox?
                        combo.SelectedIndex = 0;
                        break;
                    default:
                        combo.DataSource = null;
                        break;
                }
            }
        }
    }
}
 
Share this answer
 
v3
Comments
Honeyboy_20 30-Nov-11 8:28am    
I pass the name of comboxes so i need to loop on it
#realJSOP 30-Nov-11 11:16am    
But you don't need to. I suspect this is a one-off method in a single form. there's no point in not making it form-specific, which means you can safely use the names of the combo boxes in a switch statement. Even if you couldn't, you could just as easily create a list external of the method in question, and search that list from the recursive method.
fjdiewornncalwe 30-Nov-11 15:07pm    
I agree with John, this is the way to do this.
Here's the solution as you want ... work perfect :)

C#
private void ClearFormControls(Control c, List<ComboBox> comboxNames)
{
    foreach (Control ctrl in c.Controls)
    {
        if (ctrl is TextBox)
        {
            ctrl.Text = "";
        }
        else if (ctrl is ComboBox)
        {
            int len = comboxNames.Count;
            for (int i = 0; i < len; i++)
            {
                if (ctrl.Name == (comboxNames[i].Name))
                {
                    ctrl.Text = "Choose an item";
                    comboxNames.RemoveAt(i);
                    len = len - 1;
                    break;
                }
                else
                {

                    if (!comboxNames.Contains(ctrl))
                    {
                        // if you bind from array use this.
                        (ctrl as ComboBox).Items.Clear();
                        (ctrl as ComboBox).Text = "";

                        // If you bind from data source use this.
                        //  (ctrl as ComboBox).DataSource = null;
                    }
                }


            }
        }

        else
        {
            if (ctrl.Controls.Count > 0)
            {
                ClearFormControls(ctrl , comboxNames);
            }

        }

    }
}



calling

XML
List<ComboBox> seq = new List<ComboBox>();
           seq.Add(comboBox_AccountCategory);
           seq.Add(comboBox_CompanyName);
           seq.Add(comboBoxAge);
           seq.Add(comboBoxSalary);
           ClearFormControls(this, seq);
 
Share this answer
 
Comments
Honeyboy_20 30-Nov-11 20:23pm    
Thanks for your effort .. work with me without any problem
Your solution implies a need for a recursive parsing of the container object passed in to parameter Control 'c.

And, what you pass in here in the ArrayList parameter 'comboxNames is not the names of the ComboBoxes (that would imply strings), but a collection of references to the instances of ComboBox.

So, if you have, in advance, all the references to the ComboBoxes that require handling by the code you show here, I see no need for recursion to handle them.

On the other hand, your requirement, evidently, to set every TextBox's Text property to an empty string, appears to require recursion.

So I would decompose this into two solutions: a non-recursive one that just iterates your collection of ComboBox references and does what you need to do ...

And a second, recursive, solution to reset all the TextBoxes.

Now, if you had a situation where a bunch of TextBoxes all had the same name, but were all in different container objects, then .NET (2.0 and later) gives you the Controls.Find(string "ControlName," bool SearchAllChildren) method to return an array, and, obviously it can do a recursive search for you.

If you really had a whole bunch of TextBoxes, many in the same container (where they obviously must have unique names), or scattered across many containers, nested to whatever depth ...

I'd consider doing a recursive "walk" of the Form in the Form Load EventHandler where I built up a List<TextBox> to use later on for resetting them again, without possibly having to recurse more than once.

However, if you are adding controls at run-time, then that means that, as you create the new Controls, you need to add them to the list of either ComboBoxes, or TextBoxes ... if they require being handled as you show here. And, if they can be deleted at run-time: well, you figure that one out :)

best, Bill
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900