Question

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)

Was it helpful?

Solution 3

Thank you all for your inputs..

Above C++ code uses, 8 bit LPSTR as a buffer. Problem with rich text box is it doesn't support Unicode - 8 bit characters... It supports only - 16 bit characters

Instead of a rich text box, we can use a text box in read only mode with Unicode - 8 bit..

    private void button1_Click(object sender, EventArgs e)
    {
        string fullPath = "C:\\report.txt";
        byte[] byteArra = System.IO.File.ReadAllBytes(fullPath);
        textBox1.Text = Encoding.UTF8.GetString(byteArra);
    }

OTHER TIPS

I think there may be different tab settings(i mean the no. of spaces for one tab)

You should format your string properly in the file while saving the data. Problem is the tab sizes. As you have already mentioned, your file contains white spaces in combinition with tabs. So different tab sizes (for maybe notepad and visual studio strings) are the main problem.

One possible remedy could be saving your file in fully formated way (without using white spaces) only using tabs.

If you are writing file in .NET see this link to save file.

Use the following simple code--in Visual C++ for achieving your result.It just transfers the file to RTB text.I have been struggling two days for this.I achieved it by just coverting your C# code to C++/CLR

String^ fullPath = "C:\\report.txt";
richTBox1->LoadFile(fullPath, RichTextBoxStreamType::PlainText);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top