问题
I'm having a problem with storing amazing amounts of text in a rich TextBox.
I'm trying to read a text file fairly big( anywhere from 90mb to 450mb), and put what I've read in a rich textbox. It works in a simple program, but when I do in a complicated program I get an OutOfMemory exception.
One thing to note is that when I exit my simple program, I get an OutOfMemory exception right before the program returns 0.
Here is my simple program's code:
array<String^>^ strArray;
StreamReader^ sr;
String^ dummyStr;
int dummyInt;
sr = gcnew StreamReader("C:\\testsize.txt");
while( (dummyStr = sr->ReadLine() )!= nullptr)
{
dummyInt++;
}
sr->Close();
sr = gcnew StreamReader("C:\\testsize.txt");
strArray = gcnew array<String^>( dummyInt );
for(int i=0; i < strArray->Length; i++)
{
strArray[i] = sr->ReadLine();
}
richTextBox1->Lines = strArray;
I have a similar snippet of code in my project, and the exception pops up when I do the richTextBox1->Lines = strArray line.
I have read the documentation of the rich textbox, and it says the max limit is 64KB worth of characters, but that makes sense halfway through, as I can load the text, but I guess the program has a problem dumping it afterwards.
Any ideas? I have been trying to find maybe some custom controls without a limit, but to no success so far.
回答1:
As far as dumping a huge amount of text into a rich edit, this usually is going to be excruciatingly slow, take notepad for example, try and open a 2MB file with it. I think the way more advanced text editors deal with these is by a 'virtual control' I know these are often used with list controls, and I would think with text boxes as well. They basically act/function the same way as your normal everyday control but without trying to render oodles of text at a time, they have virtual space 'off the screen space'.
As far as your out of memory issue... I'm confused you say the error happens at the last line of your sample code when you try and dump your text to it. You also mention that the limit is 64KB so now assuming your file is huge like you say... it makes sense you get an error there you've tried to dump more than 64KB text into a 64KB limited box. Am I missing something?
Edit I reread some of the question I see what you are asking now, so in the simple program you get an error after everything is loaded done, when the program exits. Throw a debug point into your destructors, and see exactly where this error happens.
Edit 2 Now that I know what system you are on, I went and had a look, the documentation is a little more complex than 64K limit. Which first of all isn't referring to 64 KB but rather 64000 characters. Also note that you can change this limit as you please. Secondly if you are streaming with SF_TEXT and not SF_RTF this limit has no effect, which I would imagine is what is going on behind the seen of the .NET interface.
来源:https://stackoverflow.com/questions/1139006/richtextbox-limit-of-characters