首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

C# 创建窗口句柄时出错。 待大神.

2013-02-28 
C#创建窗口句柄时出错。在线等待大神......!!!!!!private TableLayoutPanel _lvwTable null public void

C# 创建窗口句柄时出错。 在线等待大神......!!!!!!
private TableLayoutPanel _lvwTable = null; 
public void BindTable()//每1秒调用这方法刷新一次
 {//加过程序锁还是一样
                    DisposeControls(_lvwTable);
                    _lvwTable.Controls.Clear();
                    int useState, j = 0;
                    Label lbl = null;
                    for (int i = 0; i < 30; i++)
                    {
                        lbl = new Label();
                        lbl.Width = 120;
                        lbl.Height = 120;
                        lbl.Cursor = Cursors.Hand;
                        lbl.Margin = new Padding(3, 3, 3, 3);
                        lbl.ContextMenuStrip = this.cmsLvwItem;
                        lbl.TextAlign = ContentAlignment.MiddleCenter;
                        lbl.Font = new Font("微软雅黑", 12);
                        lbl.Image = _StateColor[1];
                        _lvwTable.Controls.Add(lbl, (j % _Column), (j / _Column));
//刷新3分钟左右就报错乐/   创建窗口句柄时出错。
                    }
}
 private void DisposeControls(Control cParent)
{
            foreach (Control c in cParent.Controls)
            {
                c.Dispose();
            }
} c# 句柄 动态生成控件
[解决办法]
UserControl使用完后Dispose,在Application.DoEvents后加上一句“ctl.Dispose();”


[解决办法]
                    DisposeControls(_lvwTable);
                    //_lvwTable.Controls.Clear();


 private void DisposeControls(Control cParent)
{
            List<Control> ctrls = new List<Control>();
            foreach (Control ctrl in Controls)
            {
                ctrls.Add(ctrl);
                Controls.Remove(ctrl);
            }
            foreach (Control ctrl in ctrls)
            {
                ctrl.Dispose();
            }

这样试试看吧。

热点排行