Insert character at start of current line (vb.net)
Currently I have the code:
textbox1.text = textbox1.text.insert(textbox1.getfirstcharIndexFromCurrentLine(),";")
But this means it has to relo开发者_高级运维ad the entire textbox, which with large files is noticeable slow.
What are the alternatives?
Dim currcaretpos = TextBox1.SelectionStart
Dim currsellength = TextBox1.SelectionLength
TextBox1.SelectionStart = TextBox1.GetFirstCharIndexOfCurrentLine
TextBox1.SelectionLength = 0
TextBox1.SelectedText = ";"
TextBox1.SelectionStart = currcaretpos + 1
TextBox1.SelectionLength = currsellength
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 = ";";
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.
Instead of reading the file in as a single string, read it in as an array of strings:
ss = System.IO.File.ReadAllLines(filename)
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.
Manually do the scrolling, adding to or removing from the "textbox buffer" string as needed.
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.
精彩评论