开发者

Insert character at start of current line (vb.net)

开发者 https://www.devze.com 2022-12-19 09:42 出处:网络
Currently I have the code: textbox1.text = textbox1.text.insert(textbox1.getfirstcharIndexFromCurrentLine(),\";\")

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.

  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.

0

精彩评论

暂无评论...
验证码 换一张
取 消