簡體   English   中英

當其中有5000個日歷項目時Windows窗體日歷變慢

[英]Windows forms calendar becomes slow when there are 5000 calendar items in it

我正在使用Windows日歷控件構建一個小時注冊程序。 現在,我有一個關於其中包含許多議程項目的控件的性能問題。 我目前正在使用此測試代碼在日歷中填充5000個日歷項:

 private void test()
        {
            Random r = new Random();

            int i = 0;
            for (i = 1; i <= 500; i++)
            {
                CalendarItem _calendaritem = new CalendarItem(calendar1);
                _calendaritem._activity = "activity" + i.ToString();
                _calendaritem._project = "project" + i.ToString();
                _calendaritem._client = "client" + i.ToString();
                _calendaritem.Text = "Title" + i.ToString();
                _calendaritem._price = r.Next(0, 200);
                _calendaritem._variouscosts = r.Next(0, 1000);
                _calendaritem._Kilprice = r.Next(0, 5);
                _calendaritem.BackgroundColor = Color.Yellow;
                //_calendaritem.

                _calendaritem._note = "note" + i.ToString();

                DateTime _newdate = new DateTime(r.Next(2000, 2015), r.Next(1, 12), r.Next(1, 28));

                _calendaritem.StartDate = _newdate;
                _calendaritem.EndDate = _newdate.AddHours(5);

                _items.Add(_calendaritem);
            }

        }

日歷仍然可以處理這么多項目,但是性能確實很差。 日歷控件變得非常慢。

有人知道是什么原因造成的嗎?

順便說一句,我認為可能會導致此問題的原因是,Windows日歷使用視圖范圍僅在該視圖內加載項目。 我對此機制做了一些調整,因為否則我將無法保存和打開日歷控件中當前存在的所有可能的日歷項目日期。

例如,place items方法使用如下:

private void PlaceItems()
        {
            foreach (CalendarItem item in _items)
            {
                if (calendar1.ViewIntersects(item))
                {
                    calendar1.Items.Add(item);
                }
            }
        }

現在,我使用以下代碼:

private void PlaceItems()
    {
        foreach (CalendarItem item in _items)
        {
            //if (calendar1.ViewIntersects(item))
            //{
            calendar1.Items.Add(item);

            //}
        }
    }

在不丟失保存和打開日歷中所有日歷項的功能的情況下,是否有解決此問題的方法?

提前致謝!

各位程序員,我想我自己找到了答案。 如果我將位置項方法與視圖相交功能一起使用(與編輯之前一樣),則當日歷項超過100.000時,它也可以正常工作。

所以我以前通過直接循環通過calendar.item集合來進行保存,如下所示:

foreach (CalendarItem item in calendar1.Items)
                    {
                    }

那是行不通的,因為這樣只會保存當前視圖中的日歷項目。 但是,如果我這樣做,所有項目都會按計划保存,並且不會降低性能:

     foreach (CalendarItem item in _items)
                {
                 }

現在就可以使用! 我希望這個答案對遇到相同問題的其他人有用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM