我在窗体里面用For循环添加了1000个Label,启动的时候实在卡啊!!!!!
一分钟啊
求救

解决方案 »

  1.   

    虽然有很多提高程序加载的技巧,但是在这里,我需要告诉你的是,使用1000个Label本身就是错误的。
      

  2.   

    那能怎么办?一个大型场所可能就需要1000点来表示   1000个位置  ,这些个位置的的设备的开关需要一目了然并且很容易进行控制,我想到的比较好的标识方法就是电影院的座位表
    不然我也不会问下面的这个问题了
    http://bbs.csdn.net/topics/390512165?page=1#post-395033204
      

  3.   

    那能怎么办?一个大型场所可能就需要1000点来表示   1000个位置  ,这些个位置的的设备的开关需要一目了然并且很容易进行控制,我想到的比较好的标识方法就是电影院的座位表
    不然我也不会问下面的这个问题了
    http://bbs.csdn.net/topics/390512165?page=1#post-395033204按照你的说法,那excel需要“几千个文本框”,扫雷需要“几千个按钮”了?如果你只会拖控件,那实在没有辙。我说什么好呢,现在青鸟学生都知道扫雷怎么做。
      

  4.   

    或者你告诉我正确的做法也可以啊   在下感激不尽花一点时间找一个开放源代码的Grid控件内部是怎么实现的,如何处理绘制界面和响应滚动区域,自然你会知道怎么做。
      

  5.   

    这个你自绘就行了,根据坐标触发mouseover,mouseclick,效率很高的,画1000个也就是几毫秒的事
      

  6.   


    耐心读一下这个简单的入门:http://www.csharpwin.com/csharpspace/12492r4352.shtml
      

  7.   

    Label是最简单的控件之一,它耗费的性能不是很多。可以这样试一下。在一个控制台程序里边,我们可以添加一个Form 窗口,然后启动程序写using System.Windows.Forms;namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                var f = new Form1();
                f.FormClosed += f_FormClosed;
                f.Show();
                Application.Run();
            }        static void f_FormClosed(object sender, FormClosedEventArgs e)
            {
                Application.Exit();
            }
        }
    }
    而窗口的code-behind代码中写using System;
    using System.Drawing;
    using System.Windows.Forms;namespace ConsoleApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();      //你可以现在设计器上的设计一些内容
                go();   //然后,自己的程序添加1000个Label
            }        private void go()
            {
                var maxX = this.Width - 100;
                var maxY = this.Height - 20;
                var rnd = new Random();
                for (var i = 0; i < 1000; i++)
                {
                    var x = rnd.Next(maxX);
                    var y = rnd.Next(maxY);
                    var n = rnd.Next(100);
                    var lb = new Label
                    {
                        Text = Guid.NewGuid().ToString(),
                        ForeColor = Color.FromArgb(rnd.Next()),
                        Location = new Point(x, y)
                    };
                    this.Controls.Add(lb);
                }
            }
        }
    }可以看到,由于Label控件过于简单,因此即使写还是不写SuspendLayout语句其显示1000个Label所花时间结果都差不多(2、3秒钟)。
      

  8.   

    曹MVP的回答真够官方的,很像天朝“有关部门”的官方回答:虽然有很多提高程序加载的技巧,但是在这里,我需要告诉你的是,使用XXX本身就是错误的。 
    花一点时间找一个开放源代码的XXXX内部是怎么实现的,如何XXXX,自然你会知道怎么做。
      

  9.   

    这种应用使用ListView或者WebBrowser会不会更好呢?
    using System;
    using System.Windows.Forms;namespace WindowsFormsApplication7
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();            this.DoubleBuffered = true;
                listView1.LargeImageList = imageList1;
                listView1.Dock = DockStyle.Fill;
                listView1.View = View.LargeIcon;
                listView1.MultiSelect = false;
            }        private void Form1_Load(object sender, EventArgs e)
            {
                ListViewItem[] items = new ListViewItem[1000];            for (Int32 i=0;i<1000;i++)
                {
                    items[i] = new ListViewItem(String.Format("Room", i));
                    items[i].ImageIndex = 0;
                }            listView1.SuspendLayout();
                listView1.Items.AddRange(items);
                listView1.ResumeLayout();
            }
        }
    }
      

  10.   


    我看了  是我想要的  但是不理想啊  item之间的空间太大了  看来只能硬着头皮是用gdi+了吗
      

  11.   


    我看了  是我想要的  但是不理想啊  item之间的空间太大了  看来只能硬着头皮是用gdi+了吗
    listView就应该可以的,修改下属性就满足要求
      

  12.   

    关于ListView控件,item之间的空间太大了 自己调属性应该可以了,下面是WebBrowser的使用,WebBrowser的可调性更大:using System;
    using System.Security.Permissions;
    using System.Text;
    using System.Windows.Forms;namespace WindowsFormsApplication7
    {
        [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
        [System.Runtime.InteropServices.ComVisibleAttribute(true)]
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();            this.DoubleBuffered = true;
                this.WindowState = FormWindowState.Maximized;
                webBrowser1.Dock = DockStyle.Fill;
                webBrowser1.ObjectForScripting = this;
                webBrowser1.Navigate("about:blank");            
            }        private void Form1_Load(object sender, EventArgs e)
            {
                StringBuilder sb = new StringBuilder(2048);            sb.Append("<style type=\"text/css\">table.gridtable { font-family: verdana,arial,sans-serif;font-size:11px;color:#333333;border-width: 1px; border-color: #666666;border-collapse: collapse;}");
                sb.Append("table.gridtable td { border-width: 1px; padding: 8px; border-style: solid; border-color: #666666; background-color: #ffffff;}</style>");
                sb.Append("<table class=\"gridtable\">");            Int32 index = 0;
                for (Int32 i = 0; i < 1000; i++)
                {
                    sb.Append("<tr>");                for (Int32 j = 0; j < 10; j++)
                    {
                        sb.AppendFormat("<td><input type='button' value='Room{0}' onclick=\"external.HtmlClick('{0}')\"/></td>", ++index);
                    }
                    sb.Append("</tr>");
                }
                sb.Append("</table>");            webBrowser1.Document.Write(sb.ToString());
            }        public void HtmlClick(String s)
            {
                MessageBox.Show(String.Format("点击了按钮 {0}", s));
            }
        }
    }
      

  13.   

    只是Label的话,直接绘制会好些。
      

  14.   

    1000个label,这么多啊,不过可以尝试优化,减少label个数
      

  15.   

    这个的话和gridview差不多  还是listView比较合适
      

  16.   


    设个是我想实现的效果...256个label  效率还是可以的  更多的label就不行了消耗时间呈现几何倍数
      

  17.   


            private void Form1_Load(object sender, EventArgs e)
            {            Thread oThread = new Thread(new ThreadStart(proBox));
                oThread.Start();
             }
            delegate void ProBoxHandle();
            private void proBox()        
            {       
                   if (this.InvokeRequired)
                        {
                            ProBoxHandle p = new ProBoxHandle(proBox);
                            this.Invoke(p);
                        }
                        else
                        {
                            DevExpress.XtraEditors.LabelControl _lable;
                            for (int i = 0; i < 400; i++)
                            {
                                int iNumber = i + 1;
                                _lable = new DevExpress.XtraEditors.LabelControl();
                                _lable.Appearance.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
                                _lable.AutoSizeMode = LabelAutoSizeMode.None;
                                _lable.Size = new System.Drawing.Size(24, 24);
                                _lable.BackColor = Color.Beige;
                                _lable.Name = "lblCirButton_" + iNumber.ToString();
                                _lable.Text = iNumber.ToString();
                                this.AreaLayoutPanel.Controls.Add(_lable);
                            }
                        }
            }
      

  18.   

    参考18楼
    把 Controls.Add ==> Controls.AddRange 
      

  19.   

    GDI+画图填充PictureBox里面,点击事件取色。
      

  20.   

    干嘛不用一个表格控件,往里面填写数就行了,新建100个Label都是错的做法。
      

  21.   

    实测就算是1000个label一点也不卡,不SuspendLayout不开双缓冲,启动、拖动窗体和缩放都试了,不知楼主的1000个是怎么弄的
            public Form1()
            {
                InitializeComponent();            var p = new Panel();
                p.Dock = DockStyle.Fill;
                p.AutoScroll = true;
                Controls.Add(p);            for (int i = 0; i < 100; i++)
                {
                    for (int j = 0; j < 10; j++)
                    {
                        Label l=new Label();
                        l.AutoSize = true;
                        l.Left = j * 50;
                        l.Top = i * 30;
                        l.Text = (i * j).ToString();
                        p.Controls.Add(l);
                    }
                }
            }
      

  22.   

    一个界面用得着那么多label嘛 
      

  23.   


    这真的不卡?我是net2.0的
    用的是dev的控件
      

  24.   

    顶27楼的,大量控件的添加用addrange效率比一个个add高多了