![](/img/trans.png)
[英]C# - Custom control with panel on it, how do I make the IDE add items to the panel?
[英]How to deal with create window handle exception when add custom control to a panel in C#?
目前,我有一个程序来显示一些项目信息。 我所拥有的是一个可行使不同类别的com box
,一个面板可在com box
按选择显示项目。
我创建了自己的自定义控件进行显示,但是当我将其动态添加到面板时,它会导致Win32 Exception
,即“ Create Window handle Error
。
经过几次测试,我注意到一旦面板总共列出了1800个自定义控件,就会发生异常。 有谁能解决这个问题? 谢谢。
private void DisplayItems(List<ITEM_DATA> ItemList)
{
DisposeControls();
int total = ItemList.Count;
ItemDisplayer itemDisplayer = null;
Application.DoEvents();
for (int i = 0; i < total / 4 + 1; i++)
{
for (int j = 0; j < 4; j++)
{
int m = (i * 4) + j;
if (m >= total)
{
return;
}
itemDisplayer = new ItemDisplayer(ItemList[m], ref labItemName);
itemDisplayer.Size = new Size(240, 80);
itemDisplayer.Location = new Point(240 * j, 80 * i);
itemDisplayer.Name = "itemDisplayer" + Convert.ToString(m);
pnlItems.Controls.Add(itemDisplayer);
}
}
}
只想确认一下。 花了一段时间后才弄清楚此问题发生在哪里。 我意识到用户对象已达到限制10000。而我只是通过正确配置用户控件解决了该问题。
我在我的自定义控件类中重新实现了dispose方法。
// Flag: Has Dispose already been called?
bool disposed = false;
// Instantiate a SafeHandle instance.
SafeHandle handle = new SafeFileHandle(IntPtr.Zero, true);
// Public implementation of Dispose pattern callable by consumers.
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
// Protected implementation of Dispose pattern.
protected virtual void Dispose(bool disposing)
{
if (disposed)
return;
if (disposing) {
handle.Dispose();
// Free any other managed objects here.
//
}
// Free any unmanaged objects here.
//
disposed = true;
}
根据这里输入链接描述
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.