Click here to Skip to main content
Rate this: bad
good
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 5:08am
Comments
richcb at 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.
Sergey Alexandrovich Kryukov at 5-Nov-12 11:20am
   
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
0 Sergey Alexandrovich Kryukov 363
1 OriginalGriff 354
2 George Jonsson 248
3 Shemeemsha RA 148
4 Animesh Datta 130
0 OriginalGriff 6,179
1 Sergey Alexandrovich Kryukov 5,616
2 CPallini 4,770
3 George Jonsson 3,400
4 Gihan Liyanage 2,522


Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 5 Nov 2012
Copyright © CodeProject, 1999-2014
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