从控件中删除控件不会从窗体中消失 [英] Control does not disappear from form when removed from Controls

查看:78
本文介绍了从控件中删除控件不会从窗体中消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

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();
                    }
                }


在代码的另一部分中,基于列表框中的选择,将数据网格放置在表单上.网格被创建并放置在窗体上彼此顶部相同的位置.然后,基于列表框中的选择,将与该选择关联的网格带到顶部进行查看和交互.按下重置按钮时,我有上面的代码.它将清除表单中的所有数据网格.当我单步执行代码时,实际上确实已将其全部删除,但是,此时位于顶部的网格不会从表单中消失.我尝试将代码的"this.Refresh()"行移至for循环之后,且未进行任何更改.我只是不明白为什么会从表单中删除除顶部网格之外的所有网格.


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.

推荐答案

尝试使用this.Invalidate()而不是this.Refresh(),如果所有其他方法都失败了,您可以做:
Try this.Invalidate() instead of this.Refresh(), if all else fails you can do :
Controls[i].Visible = false;


这篇关于从控件中删除控件不会从窗体中消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆