We have a VB6 program that does some string processing in a loop (approximately 500 times) and appends info to a textbox. Each iteration on the loop includes basic operations like Trim, Len, Left, Mid, etc, and finally appends the string to a textbox (the whole form is still invisible at this point). Finally, after the loop, the code calls Show on the form.
On Windows XP, these 500 loops take about 4 seconds. On Windows 7, the exact same code runs in about 90 seconds.
Any suggestions on how to fix this?
Thanks.
I’m guessing you append the text box on each loop iteration… If you can, store everything in a variable and append it to the
TextBoxonce after the loop is finished. Displaying text in a text box takes a lot of time in VB6.EDIT:
After further investigation and testing, I came to a conclusion that performance on directly assigning strings to the
Textproperty of aTextBoxdegrades dramatically when the length of the control reaches maximum. The maximum on my PC is 65535 for some reason, even though according to MSDN itsBasically what seems to be happening, if you keep adding text to the
TextBoxeach iteration, it isn’t that slow until you reach maximum. What’s even more puzzling, when you try to add text beyond the maximum, there will be no errors, but performance degrades significantly.In my test loop I go from 0 to 12773 I have this:
Text2.Text = Text2.Text + CStr(a) + " "So when the loop is completed in 4 seconds, the
Text2.Textis 65534 characters long. Now, when I double the loop to go beyond the maximum allowed length of theTextBox, it takes three times as much time to complete it.12773 – 4 seconds
12773*2 – 16 seconds
After realizing this, my first thought was to replace the
TextBoxwith aRichTextBox. But the performance of the latter is even worse. This is assuming you update it every iteration.It seems that you are stuck with a dilemma – suffer slow performance or change the code to update text box only once after the loop is completed. Further, due to
TextBox‘s maximum length limit, I recommend switching to aRichTextBoxor depending on the purpose of this – some other object.I hope my findings are helpful – it has certainly been fun finding out all these little programming quirks.