2

I've made a custom control, it's a FlowLayoutPanel, into which I put a bunch of other custom controls (just Buttons, each with three Labels and a PictureBox overlayed)

It works ok with around 100 buttons, but bump that up to 1000 and it's in trouble. Bump that up to 5000 and it just dies after 20 seconds.

I have very little custom code, and I make judicious use of suspend and resume layout.

So what am I doing wrong? I'm sure my (fairly rapid) computer should be able to handle a few thousand buttons and labels.

(I'm fairly new to C# GUI stuff, so perhaps I should be doing things completely different anyway.)

Edit 1:

This is pretty much the only custom code at the moment:

flowLayoutPanel1.SuspendLayout();
foreach (DataRow row in dt.Rows) // dt is from a DB query
{
    flowLayoutPanel1.Controls.Add(new PersonButton(row));
}
flowLayoutPanel1.ResumeLayout();

and in the PersonButton constructor:

this.label1.Text = row["FirstName"].ToString().Trim() + " "
    + row["Surname"].ToString().Trim();

(There should also be a picture attached but I'm not sure if anyone can see it.)

Edit 2:

I guess I really should be using a DataGridView, or ListView, but I wanted more than just a line of text and a small icon per row; I wanted it to look similar to the downloads view in firefox (Ctrl + J). (See screenshot)

Thanks very much for all your input, BTW. I guess I'll have to rethink...

alt text http://img156.imageshack.us/img156/1057/capture.png

aidan
  • 9,310
  • 8
  • 68
  • 82

7 Answers7

15

Can a C# WinForm app handle 1000 instances of any type of control? I am no WinForm Guru, but what you are expecting from your application might be unreasonable.

The fact that you want to show 1000+ controls of any type might be a sign that you are approaching the design of your software from the wrong direction.

Matthew Ruston
  • 4,282
  • 7
  • 38
  • 47
  • I was worried that would be the answer :) – aidan Feb 25 '09 at 21:27
  • 2
    Agreed! Why would you want to display 1000 controls on a form? – Rune Grimstad Feb 25 '09 at 21:27
  • +1 Matthew, how can a user be expected to keep track of 5000 different buttons? – scottm Feb 25 '09 at 21:28
  • 1
    +1. This falls under Raymond Chen's "If you have to ask, you're probably doing it wrong" rule: http://tinyurl.com/d27sdt. Usability is key: There's pretty much never a reason to display 1000+ controls on a page. That much data WILL overwhelm the user. – John Rudy Feb 25 '09 at 21:31
  • 2
    You guys never played minesweeper at expert level right ? – Mibou Dec 30 '14 at 11:30
2

You'll have to post some of the layout code or we won't be able to help much/at all.

Also, your #1 best bet is to profile your code. Profiling is the only surefire way to find out what exactly is performing slowly in your code. In my experience, this is especially true of UI code.

JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
  • Should I post the layout code here in the comments? flowLayoutPanel1.SuspendLayout(); foreach(DataRow row in dt.Rows) { flowLayoutPanel1.Controls.Add(new PersonButton(row)); } flowLayoutPanel1.ResumeLayout(); That's the only custom code. Except the PersonButton constructor which alters the labels. – aidan Feb 25 '09 at 21:26
1

[Necromantic mode = ON]

You have 1000 row of data, but you only can show a few of them, so create only the controls that will be visible, and reuse them changing its content with the new data when you scroll.

Blau
  • 5,742
  • 1
  • 18
  • 27
1

At 1000+ buttons your probably running perilously low on GDI resources and/or raw handles for your application.

Not sure what your application is supposed to do but a grid or a combo box may be the better option here.

Mike Marshall
  • 7,788
  • 4
  • 39
  • 63
1

It sounds like you seriously need to rethink your interface.

Like others have mentioned, that number of controls on a form will not be usable.

However I have done some experimenting looking at time to create new controls in code, even using reflection, and found that several hundred data-bound controls created on the fly in a flow-layout panel should be created in 1 to 2 seconds.

Posting more code samples may help get a better answer.

More info: I have just rerun my timing test again, 300 controls took 0.5 seconds, 400 took 1.9 seconds, 600 took 3 seconds, 1000 took 6 seconds.
It seems that there is a limit somewhere between 300 and 400 where resources start to get over utilised.

benPearce
  • 37,735
  • 14
  • 62
  • 96
1

The layout logic required for 5k controls will be too much for any type of wrapping panel. You might want to look into a different type of control designed the hold thousands of entries - something like a DataGridView.

The DataGridView has several different column types available that should work for the type of data you're displaying (images, buttons, labels). Since your database query looks to return a DataTable, you could simply bind that directly to your DataGridView and remove the loop.

BadCanyon
  • 3,005
  • 19
  • 17
-1

Try this; drop this method in your code.

protected override CreateParams CreateParams { get { CreateParams cp = base.CreateParams; cp.ExStyle |= 0x02000000; return cp; } }