Hoi guys. I fly between London and Zuerich on Fridays so have only now found
time to sit down and mull over all which has been written. I have found a
solution to my problem which I will elaborate on in a short while.
Just to clear things up. I am using .NET 1.1, using a standard ListBox, NOT
Sorted, do use BeginUpdate () and EndUpdate () and simply load text lines in
from a text file and store them as STRING items in the ListBox using the Add
() method. I did try AddRange (), passing in a pre-loaded array, but this
actually took longer than simply adding the elements one at a time. Also
tried assingning a DataSource member to the array with similar delays. The
delays do increase, I assure you, meaning adding the last N elements to a
ListBox does indeed take much longer than adding the first N. My example uses
strings of 500+ characters and my tests used 40,000 items, I displayed an
increment (with Refresh ()) to a Progress Bar every 4,000 calls to Add ().
Visually the slow down in Progress Bar updates is dramatic.
What I did find however is that undertaking Add ("") really flies with no
degraded performance.
So, to my solution. I was in any case implementing my own DrawItem () handler
for my listbox, displaying the text in different colours based on content. So,
whenever I actually want to add an element to a ListBox I actually add it to
a standard fixed size string array instead. I add to the ListBox Item
collection the NULL string (ie listbox.Add ("")); In the DrawItem handler,
instead of retrieving the string to display from the ListBox Item collection,
I fetch it from the string array instead. I generalised this technique as one
of my ListBox displays increases in size dynamically and I do not know how
big it may become. I use the ListBox because logically this is the Control
whose functionality best maps to my requirement. I enclose some code snippets
just in case anyone may find this of use. I would like to thank ALL you guys
for your comments, and hope our paths cross in the virtual world at some
other time in the future. Many mant thanks guys (and maybe gals too).
public partial class Form1 : Form
{
public ListElements le;
public int count = 0;
public Form1 ()
{
InitializeComponent ();
}
private void Form1_Load (object sender , EventArgs e)
{
le = new ListElements (listBox1);
}
private void button1_Click (object sender , EventArgs e)
{
le.Add (count.ToString ());
count++;
}
private void listBox1_DrawItem (object sender , DrawItemEventArgs e)
{
if (e.Index >= 0)
{
e.Graphics.DrawString (le [e.Index] , e.Font, Brushes.Black, e.Bounds);
}
}
private void button2_Click (object sender , EventArgs e)
{
le.Clear ();
}
}
public class ListElements
{
public const int NUM_ARRAYS = 2000;
public const int ARRAY_ELEMENTS = 10000;
public string [] [] elements = new string
[NUM_ARRAYS] [];
public int arrayElement = 0;
public int arrayNumber = 0;
public string [] activeArray;
public ListBox lb;
public string this [int i]
{
get
{
int ae;
int an;
if (i >= (NUM_ARRAYS * ARRAY_ELEMENTS))
{
return null;
}
an = i / ARRAY_ELEMENTS;
ae = i % ARRAY_ELEMENTS;
return elements [an] [ae];
}
}
public ListElements (ListBox l)
{
this.lb = l;
return;
}
public void Add (string s)
{
lb.Items.Add ("");
if (arrayElement == 0)
{
elements [arrayNumber] = new string [ARRAY_ELEMENTS];
activeArray = elements [arrayNumber];
}
activeArray [arrayElement] = s;
arrayElement++;
if (arrayElement == ARRAY_ELEMENTS)
{
arrayNumber++;
if (arrayNumber >= NUM_ARRAYS)
{
throw (new SystemException (String.Format ("No more list elements, now
added > {0} * {1} elements !", NUM_ARRAYS, ARRAY_ELEMENTS)));
}
arrayElement = 0;
}
return;
}
public void Clear ()
{
lb.Items.Clear ();
arrayElement = 0;
arrayNumber = 0;
return;
}
}
--
Message posted via DotNetMonster.com
http://www.dotnetmonster.com/Uwe/For...sharp/200606/1