Is there way to speed up displaying a lot of text in a winforms textbox?

前端 未结 7 886
天涯浪人
天涯浪人 2020-12-21 14:32

Is there way to speed up displaying a lot of text in a winforms textbox? My application reads a file (which can be as large as 20MB) and then displays it to a textbox (TextB

相关标签:
7条回答
  • 2020-12-21 14:49

    I think that a 20Mb file would have a visible delay even when opened through notepad.

    Adding to the suggestion on reading chunks of data (which is the best option for large data.. like its proven on web downloads)..

    you could open the text file in read mode as a filestream.. and then read 1mb or less of byte content in a loop .. and add the content to the textbox.

    psuedocode:

    while (not end of file) { string content = read 1024*1024 bytes from stream.. convert it into ascii encoding.

    textbox.text += content

    }

    0 讨论(0)
  • 2020-12-21 14:53

    The Text property is evil if you like to put lot of text into a TextBox. Instead you should read the file in chunks and add them by using the AppendText() function.

    If you go further and put your file read process into a BackgroundWorker that reads in the file line by line and then reports each line in ReportProgress, you could implement there the AppendText() and it should do everything much smoother.

    Update

    After some coding and testing i have to admit that the above described approach sounds good, but the TextBox needs so much rendering time after each AppendText() that this just doesn't work.

    But if you have no problem about using 3rd party controls you should take a look at Scintilla.Net. It has no problems with big text files and performs really better in these cases.

    0 讨论(0)
  • 2020-12-21 14:56

    I know this question is old at this time, but I would like to add information I consider may be helpful to others.

    If you set WordWrap=false, the performance will improve dramatically in some cases. It will still wrap long lines when a max limit is reached. I tried some text 90K long which included a long chunk of Base64 data, and the response changed from 16 seconds to 2 seconds.

    0 讨论(0)
  • 2020-12-21 15:00

    Solution with minimal change in code will be to read the whole file contents into variable, then append "chunks" of the contents to the textbox in a loop calling Application.DoEvents() in each iteration.

    You can show "Loading please wait..." message while it's still loading, the DoEvents call will ensure your application won't be "frozen" in the meanwhile.

    0 讨论(0)
  • 2020-12-21 15:08

    Have a look at the LockWindowUpdate function, don't forget to unlock once you've added the text to the textbox.

    http://msdn.microsoft.com/en-us/library/dd145034%28v=vs.85%29.aspx

    0 讨论(0)
  • 2020-12-21 15:10

    I think there is only one way - to build your own control (or maybe there is one) to display small parts of the text (like in far manager, when you open extreme large files) in your box.

    You define a "window" - for example 500 letters and when you use scrollbar to change position you change body of yourtextbox and display another part of the text.

    0 讨论(0)
提交回复
热议问题