C# WinForms中列表框中的项目数限制
我有一个WinForms应用程序,它使用一个列表框来显示项目列表。当列表框中的项目数超过150个时,我的应用程序将挂起。这是ListBox控件的属性吗,它只能容纳这么多项?如果是的话,我会要求你提供一个解决这个问题的方法 谢谢,C# WinForms中列表框中的项目数限制,c#,C#,我有一个WinForms应用程序,它使用一个列表框来显示项目列表。当列表框中的项目数超过150个时,我的应用程序将挂起。这是ListBox控件的属性吗,它只能容纳这么多项?如果是的话,我会要求你提供一个解决这个问题的方法 谢谢, Rakesh.您可以通过一个更大的数据集来备份您的列表框,并使用分页机制,或者您可以为SizeChanged添加一个事件侦听器,并在达到最大值时禁用添加。这一切都取决于您绑定的内容,如果您绑定简单的键值对,您可以立即轻松绑定10k。您可能希望尝试在循环中添加项,而不是绑
Rakesh.您可以通过一个更大的数据集来备份您的列表框,并使用分页机制,或者您可以为SizeChanged添加一个事件侦听器,并在达到最大值时禁用添加。这一切都取决于您绑定的内容,如果您绑定简单的键值对,您可以立即轻松绑定10k。您可能希望尝试在循环中添加项,而不是绑定,以查看是否有某个项挂起
for (int i = 0; i < 10000; i++)
{
listBox1.Items.Add("item:" + i.ToString());
}
for(int i=0;i<10000;i++)
{
listBox1.Items.Add(“item:+i.ToString());
}
第一个提示,始终
SuspendLayout();
// fill your lists
ResumeLayout();
第二个提示,尽可能使用AddRange
第三,创建自己的列表框可能有些过分了
public class LimitedListBox : ListBox
{
private int _maxItems = 100;
public LimitedListBox()
{
SetItems(new LimitedObjectCollection(this, _maxItems));
}
public int MaxItems
{
get { return _maxItems; }
set { _maxItems = value; }
}
/// <summary>
/// This is the only 'bug' - no design time support for Items unless
/// you create an editor.
/// </summary>
public new LimitedObjectCollection Items
{
get
{
if (base.Items == null)
{
SetItems(new LimitedObjectCollection(this, _maxItems));
}
return (LimitedObjectCollection) base.Items;
}
}
private void SetItems(ObjectCollection items)
{
FieldInfo info = typeof (ListBox).GetField("itemsCollection",
BindingFlags.NonPublic | BindingFlags.Instance |
BindingFlags.GetField);
info.SetValue(this, items);
}
#region Nested type: LimitedObjectCollection
public class LimitedObjectCollection : ObjectCollection
{
private int _maxItems;
public LimitedObjectCollection(ListBox owner, int maxItems)
: base(owner)
{
_maxItems = maxItems;
}
public LimitedObjectCollection(ListBox owner, ObjectCollection value, int maxItems)
: base(owner)
{
_maxItems = maxItems;
AddRange(value);
}
public LimitedObjectCollection(ListBox owner, object[] value, int maxItems)
: base(owner)
{
_maxItems = maxItems;
AddRange(value);
}
public int MaxItems
{
get { return _maxItems; }
set { _maxItems = value; }
}
public new int Add(object item)
{
if (base.Count >= _maxItems)
{
return -1;
}
return base.Add(item);
}
public new void AddRange(object[] items)
{
int allowed = _maxItems - Count;
if (allowed < 1)
{
return;
}
int length = allowed <= items.Length ? allowed : items.Length;
var toAdd = new object[length];
Array.Copy(items, 0, toAdd, 0, length);
base.AddRange(toAdd);
}
public new void AddRange(ObjectCollection value)
{
var items = new object[value.Count];
value.CopyTo(items, 0);
base.AddRange(items);
}
}
#endregion
}
公共类LimitedListBox:ListBox
{
私有整数_maxItems=100;
public LimitedListBox()
{
SetItems(新的LimitedObjectCollection(此,_maxItems));
}
公共整数最大项
{
获取{return\u maxItems;}
设置{u maxItems=value;}
}
///
///这是唯一的“错误”-除非
///您可以创建一个编辑器。
///
公共新限制对象集合项
{
得到
{
if(base.Items==null)
{
SetItems(新的LimitedObjectCollection(此,_maxItems));
}
返回(LimitedObjectCollection)基项;
}
}
私有void集合项(ObjectCollection项)
{
FieldInfo info=typeof(ListBox).GetField(“itemsCollection”,
BindingFlags.NonPublic | BindingFlags.Instance|
BindingFlags.GetField);
信息设置值(此,项);
}
#区域嵌套类型:LimitedObjectCollection
公共类LimitedObjectCollection:ObjectCollection
{
专用int_maxItems;
public LimitedObjectCollection(列表框所有者,int-maxItems)
:基地(所有者)
{
_maxItems=maxItems;
}
public LimitedObjectCollection(列表框所有者、ObjectCollection值、int-maxItems)
:基地(所有者)
{
_maxItems=maxItems;
AddRange(值);
}
public LimitedObjectCollection(列表框所有者,对象[]值,int-maxItems)
:基地(所有者)
{
_maxItems=maxItems;
AddRange(值);
}
公共整数最大项
{
获取{return\u maxItems;}
设置{u maxItems=value;}
}
公共新整数添加(对象项)
{
如果(base.Count>=\u maxItems)
{
返回-1;
}
返回基数。添加(项目);
}
public new void AddRange(对象[]项)
{
允许的整数=_maxItems-计数;
如果(允许<1)
{
返回;
}
int length=allowed为什么不这样解析数据库呢
int Total = yourarray.GetLength(0);
Then all you have to do is this in your new array.
double [] new = double[Total];
array.copy(Total,new);
现在您有了一个动态数组。只要数据库增长,它就会自动填充
新阵列
或者,如果您可以在数据库上执行select count语句,则可以获取总数或行数,然后将其传输到字符串。然后您可以使用该字符串来控制数组。希望这有帮助,我刚刚得到一个“内存不足”填充列表框时出现错误消息。问题与项目过多无关。我的代码中存在错误,列表框中的项目在ToString()中返回null方法。因此,Dot Net错误消息是错误的和令人困惑的。请编写代码?您是否有任何代码在填充列表框事件时对其执行长时间操作?没有。尽管Vista中的错误会导致滚动问题,如果您在列表中放置的项目超过65536项。