0

I've write a program that construct a 2D matrix from txt file, and build a winforms panel with X*Y labels wich contains a char, coordinates, color and border (if select it). it is my DrawGrid routine:

        Container.SuspendLayout();

        for (int y = 0; y < template.Matrix.GetLength(1); y++)
        {
            for (int x = 0; x < template.Matrix.GetLength(0); x++)
            {
                var curLabel = new LabelTemplate(template.Matrix[x, y].Content, x, y, spacing);                    
                _templateCells.Add(curLabel);
                Container.Controls.Add(curLabel);

            }
        }
        Container.ResumeLayout();

whit it I view a txt file in my form, and select a row or columns or area with mouse, manipulate and save new text files from it getting content and coordinates from my LabelTemplate object (extends Label).

I've always test my program with a little txt files in input. Today i've tested with a big txt file (9000 rows * 50 columns) and i've reached a maximun handles of a windows form application. (an Win32 Exception is launched during Container.Controls.Add(curLabel)). Googling i've found that the limit of controls in winforms application is 10000 handles.

Also view a lot of label on my form (if 10000 is a modificable value), performance are very bad (if i scroll container panel, i wait a lot of time to view results)! There is a way or control that help me? I think also to GDI+, but what is the right way for you? Any suggestions?

Thanks in advance

davymartu
  • 1,393
  • 3
  • 15
  • 34
  • 2
    i can recommend you to use some Grid control, WinForms DataGridView for example – Ilia Maskov Apr 16 '15 at 16:49
  • 1
    See http://www.codeproject.com/Articles/5806/Binding-a-two-dimensional-array-to-a-DataGrid – John Alexiou Apr 16 '15 at 18:11
  • I see and I've tried with DataGridView (also in virtual mode) and all it's ok. But now I've to re-write all my code to adapt it with DataGridView. It's a hard job, and i want to simply add more than 10.000 Labels to my control container. There is a way? – davymartu Apr 20 '15 at 10:54

2 Answers2

2

I think you should use DataGridView control.

If your amount of data is too big, you can limit the number of items and add some control to select the start of the region that you are viewing (like a NumericUpDdown or a TrackBar). Every time you change the start index, you reload your data to the DataGridView.

Sample of how to fill a DataGridView from an array: "How do I show the contents of this array using DataGridView?".

Another solution would be to use WPF, which has built-in UI Virtualization, therefore supports much bigger datasets without having any performance impact.

Community
  • 1
  • 1
Pedro77
  • 5,176
  • 7
  • 61
  • 91
0

It is not practical to use labels for cells of a grid-like control. As agent5566 suggested, you can use DataGridView control for a fast approach or if you want full control and better performance, you can use a single UserControl and paint everything on it, handle keystrokes, simulate focus on cells (if needed) and so on.

Mehrzad Chehraz
  • 5,092
  • 2
  • 17
  • 28