I need to load a text file into the RichTextBox in C#. Loading of a text file into the rich text box should be done in the C++ way.
Is there any way of loading a file into the rich text box, as like the below C++ code?
Explanation for the below C++ code:
Here the handle is VC++ Text box handle, allocating memory for the text box handle.Based on the allocated memory, contents from the buffer is loaded into the text box in C++.
How to implement this behaviour in C#?
C++ Code:
length = (UINT) file.GetLength();
editHandle = pCEdit->GetHandle();
if (LocalReAlloc(editHandle, length + 1, LHND) == NULL )
{
file.Close();
return SF_MEM_ALLOC_ERROR;
}
// read file into the editBuffer
file.Read( (editBuffer =
(LPSTR) LocalLock(editHandle)), length );
editBuffer[length] = 0;
LocalUnlock( editHandle );
// Write it to the Text box, from the buffer
pCEdit->SetWindowText(A2T(editBuffer));
The above code doesn't have any alignment issues.
For the above C++ code, I just used the below lines of C# code
C# Code:
private void button1_Click(object sender, EventArgs e)
{
string fullPath = "C:\\report.txt";
richTextBox1.LoadFile(fullPath, RichTextBoxStreamType.PlainText);
}
But this way of loading in Rich Text box results in alignment problem.
Text file which I load, contains lot of white spaces and tabs.
How to allocate memory for a rich text box in C# based on the file size it loads ?
for checking the alignment issue. pls find the screenshot in the below link
Output for the C# Code:
(source: pictureupload.us)