Click here to Skip to main content
12,243,058 members (46,444 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C#
I have the following code:
for (int i = Controls.Count-1; i > 0; i--)
            {
                if (Controls[i].Name.Length > 7)
                {
                    if (Controls[i].Name.Substring(0, 7) == "NewGrid")
                    {
                        Controls.Remove(Controls[i]);
                        this.Refresh();
                    }
                }
In another part of the code datagrids are place on the form based on the selection in a listbox. The grids are created and placed on the form in the same place right on top of each other. Then based on the selection in the listbox the gird associated with that selection is brought to the top for viewing and interaction. I have the code above when a reset button is pressed. It clears all the datagrid from the form. When I single step through the code they are indeed all removed, however which ever grid is on top at the time does not disappear from the form. I have tried moving the "this.Refresh()" line of code to after the for loop with no change. I just don't understand why all the grids would be removed from the form except the one on top.
Posted 5-Nov-12 6:08am
Comments
richcb 5-Nov-12 11:15am
   
It appears to me that one control would always be left because you have i=Controls.Count-1. That would always leave one control because it is accounting for all controls but one.
   
It would disappear if you called Remove, but you did not. Execute it under debugger; and you will be able to see everything. You always need to use debugger before asking questions like that, as well as in many other cases.
--SA

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Try this.Invalidate() instead of this.Refresh(), if all else fails you can do :
Controls[i].Visible = false;
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160426.1 | Last Updated 5 Nov 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100