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秒钟)。
不然我也不会问下面的这个问题了
http://bbs.csdn.net/topics/390512165?page=1#post-395033204
不然我也不会问下面的这个问题了
http://bbs.csdn.net/topics/390512165?page=1#post-395033204按照你的说法,那excel需要“几千个文本框”,扫雷需要“几千个按钮”了?如果你只会拖控件,那实在没有辙。我说什么好呢,现在青鸟学生都知道扫雷怎么做。
耐心读一下这个简单的入门:http://www.csharpwin.com/csharpspace/12492r4352.shtml
{
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秒钟)。
花一点时间找一个开放源代码的XXXX内部是怎么实现的,如何XXXX,自然你会知道怎么做。
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();
}
}
}
我看了 是我想要的 但是不理想啊 item之间的空间太大了 看来只能硬着头皮是用gdi+了吗
我看了 是我想要的 但是不理想啊 item之间的空间太大了 看来只能硬着头皮是用gdi+了吗
listView就应该可以的,修改下属性就满足要求
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));
}
}
}
设个是我想实现的效果...256个label 效率还是可以的 更多的label就不行了消耗时间呈现几何倍数
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);
}
}
}
把 Controls.Add ==> Controls.AddRange
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);
}
}
}
这真的不卡?我是net2.0的
用的是dev的控件