views:

253

answers:

3

Currently I have the code:

textbox1.text = textbox1.text.insert(textbox1.getfirstcharIndexFromCurrentLine(),";")

But this means it has to reload the entire textbox, which with large files is noticeable slow.

What are the alternatives?

+1  A: 

You could check if pasting the text is faster:

textbox1.SelectionStart = textbox1.GetFirstCharIndexOfCurrentLine();
textbox1.SelectionLength = 0;
textbox1.Paste(";");

Edit:
As the textbox isn't a textbox after all, but a richtextbox, the Paste method works differently. You can put the text in the clipboard and paste it, or use the SelectedText property instead:

textbox1.SelectedText = ";";
Guffa
Thanks I will try this out.
Jonathan
Thanks you answer helped quite a bit but I think there a few differences between C# and vb.net that mean it doesn't work completely
Jonathan
@Jonathan: There shouldn't be any practical differences at all. Just remove the semicolons at the end of the lines, at it's VB code.
Guffa
well the arguments for the paste function is for the data format not what is being pasted, and it's textbox1.get firstcharindexOFcurrentline()
Jonathan
@Jonathan: Right... I inherited the typo from your code... ;) The parameter to the Paste function is not a data format, it's the text to paste. Se: http://msdn.microsoft.com/en-us/library/ms160635%28VS.85%29.aspx
Guffa
Well that's what it say when I try to compile, maybe I forgot to mention that I'm using a richtextbox called textbox1? Thanks though very much for you answer.
Jonathan
@Jonathan: I see, I assumed that it was a textbox as you said that it was a textbox (not because it's named textbox1)... A richtextbox works a little differently. See my edit above.
Guffa
sorry, because I called it textbox1 I think of it as a textbox, so thats why I wrote textbox. My fault.
Jonathan
A: 

Concatenating long strings is painfully slow. Using a richTextBox instead of a TextBox will make the user interface a lot faster for large strings, but that doesn't help much with programmatic text changes.

Here is one way that will speed up changing large strings in a text box, but it unfortunately is kind of messy.

  1. Instead of reading the file in as a single string, read it in as an array of strings:

    ss = System.IO.File.ReadAllLines(filename)
    
  2. Only assign a string about three times the height to the textbox to the textbox, concatenating the lines you read in step one and adding a crlf.

  3. Manually do the scrolling, adding to or removing from the "textbox buffer" string as needed.

  4. Reflect changes made by the user in the textbox buffer and the original lines (ss).

This is pretty cumbersome, but it will speed up text box handling of an 8 meg file/string, for example, by a factor of a few hundred.

xpda
the text is pasted into the textbox not opened.
Jonathan
+1  A: 
Dim currcaretpos = TextBox1.SelectionStart
Dim currsellength = TextBox1.SelectionLength
TextBox1.SelectionStart = TextBox1.GetFirstCharIndexOfCurrentLine
TextBox1.SelectionLength = 0
TextBox1.SelectedText = ";"
TextBox1.SelectionStart = currcaretpos + 1
TextBox1.SelectionLength = currsellength
Jonathan
+1 - used this to insert a "time stamp" in a textbox (Set to multiline).
Jeff O