Note that there are some explanatory texts on larger screens.

plurals
  1. POOpen file in rich text box with C#
    text
    copied!<p>This question have been answered. I recommend sumit_programmers solution below. For now, I've removed my code, thinking it's more confusing than helpful. When I've developed it a bit further, perhaps I'll post my code here, with some comments.</p> <p>You may also be interested in the answer to the question <a href="https://stackoverflow.com/questions/3751929/save-text-from-rich-text-box-with-c">Save text from rich text box with C#</a>. There is an answer that reminds of the accepted answer to this question. The code should work, but it's written by me, so there may be some errors or missing information.</p> <hr> <p>Update: I have improved the code a bit (at least I think so). "Encoding.Default" seems to work with most common encodings, like ANSI. If the encoding is UTF-8 without byte order mark (BOM), it seems "Encoding.Default" doesn't work, though. For more information, go to <a href="http://www.informit.com/guides/content.aspx?g=dotnet&amp;seqNum=163" rel="nofollow noreferrer">informit.com/guides</a>. Here's the code I'm using right now:</p> <pre><code>private void fileOpen_Click(object sender, EventArgs e) { using (OpenFileDialog dlgOpen = new OpenFileDialog()) { try { // Available file extensions dlgOpen.Filter = "All files(*.*)|*.*"; // Initial directory dlgOpen.InitialDirectory = "D:"; // OpenFileDialog title dlgOpen.Title = "Open"; // Show OpenFileDialog box if (dlgOpen.ShowDialog() == DialogResult.OK) { // Create new StreamReader StreamReader sr = new StreamReader(dlgOpen.FileName, Encoding.Default); // Get all text from the file string str = sr.ReadToEnd(); // Close the StreamReader sr.Close(); // Show the text in the rich textbox rtbMain rtbMain.Text = str; } } catch (Exception errorMsg) { MessageBox.Show(errorMsg.Message); } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload