Limit the number of items in a list in WinForms

I have a WinForms application that uses a list to display a list of items. My application freezes when the number of items in the list exceeds 150 items. Is this a ListBox control that it can only contain a lot of elements? If so, I ask you to provide a solution to this problem.

Thanks, Rakesh.

+3
source share
5 answers

It all depends on what you bind, if you bind pairs of simple keys, you can instantly bind 10k to the lung. You might want to try adding elements in a loop instead of snapping to see if there is a specific element on which it hangs.

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

, SizeChanged , .

+1

, ..

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
}
+1

.

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

, . , , .

, , , . . ,

0

I got an "out of memory" error message when populating the list. The problem was not related to too many items. There was an error in my code, and the items in the list box returned null in the ToString () method. Therefore, the Dot Net error message was incorrect and confusing.

0
source

Source: https://habr.com/ru/post/1732949/


All Articles