English 中文(简体)
WinForms 中列表框中项目数量的限制
原标题:Limit on number of items in list box in WinForms
  • 时间:2010-02-16 03:26:54
  •  标签:
  • c#

我有一个WinForms应用程序,它使用列表框来显示项目列表。当列表框中的项目数量超过150个时,我的应用程序会挂起。这是ListBox控件仅能容纳很少数量项目的属性吗?如果是这样,我想要请求您为此问题提供解决方案。

Thanks, Rakesh.

最佳回答

这一切取决于你绑定的内容,如果你要绑定简单的键值对,你可以很容易地即时绑定10k。你可以尝试在循环中添加项目而不是绑定,看看是否有某个项目会使其停顿。

for (int i = 0; i < 10000; i++)
{
     listBox1.Items.Add("item:" + i.ToString());
}
问题回答

您可以根据更大的数据集来支持您的列表框,并使用分页机制,或者您可以添加一个 SizeChanged 事件侦听器,并在达到最大值时禁用添加。

第一个提示,总是...

SuspendLayout();
// fill your lists
ResumeLayout();

第二个提示:尽可能使用 AddRange。

第三,虽然可能有点过头,但是创建自己的ListBox...

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
}

为什么不要让你把自己的数据库同起来。

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

Now you have an array thats dynamic. Anytime your database grows it automatically populates the new array.

如果您可以在您的数据库上执行选择计数语句,您可以得到行的总数,然后将其传递给一个字符串。然后您可以使用这个字符串来控制数组。希望这可以帮到您。

当往列表框添加项目时,我刚刚收到了一个“内存不足”的错误提示。问题与项目太多无关。我的代码中有一个错误,列表框中的项目在ToString()方法中返回null。因此,Dot Net的错误提示是错误和令人困惑的。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签