Problem loading RTF file in windows richTextBox

I am trying to upload files to windows froms (vs 2010) richTextBox, but only the first line of the file is loaded. I use:

// Create an OpenFileDialog to request a file to open. OpenFileDialog openFile1 = new OpenFileDialog(); // Initialize the OpenFileDialog to look for RTF files. openFile1.DefaultExt = "*.rtf"; openFile1.Filter = "RTF Files|*.rtf"; // Determine whether the user selected a file from the OpenFileDialog. if (openFile1.ShowDialog() == System.Windows.Forms.DialogResult.OK) { // Load the contents of the file into the RichTextBox. rtbTest.LoadFile(openFile1.FileName, RichTextBoxStreamType.PlainText); } 

I tried using rtf files created in a word or slot and tried to save .cs files as .rtf without any success.

Any help is appreciated.

+4
source share
9 answers

Good,

It seems that the whole rtb.LoadFile () thing is not working for yah. Could you try downloading the file this way ?:

 using(var of = new OpenFileDialog()) { of.DefaultExt="*.rtf"; of.Filter = "RTF Files|*.rtf"; if(of.ShowDialog() == DialogResult.OK) rtb.Rtf = System.IO.File.ReadAllText(of.FileName); } 

Hope this helps.

+2
source

I don't think the cs file really is rtf. Try using LoadFile overload with stream type e.g.

 rtbTest.LoadFile(openFile1.FileName, RichTextBoxStreamType.PlainText); 

Other than that, are you sure the rich text box is larger than the first line?

Edit

I tried. I used windows forms in vs2010 (I think you are using windows forms, but not 100% sure). I created a windows forms project and saved Project.cs as rtf. I added a button and a RichTextBox to the button click handler. I added the code from the question. In fact, this was an exception when I downloaded Program.rtf because it was not in the correct format. I added the RichTextBoxStreamType.PlainText argument to the LoadFile call, and it worked. He showed the whole file.

+1
source

How did you initially save the RTF file in the first place? I agree with Mike Two, the file has things that are not really RTF.

You can verify the correct file upload using Wordpad, which I use when working with RTF files.

Update:

One exploration method you can try is this: after loading the file into the RichTextBox, check that the debugger gives the RichTextBox.Rtf property โ€” you should see all the RTF text, including formatting. If this is really โ€œall there,โ€ then you know that you are reading the file correctly.

My concern is that you are trying to view a code file saved as RTF. This obviously should not be a problem, however I recommend saving a very simple RTF file, possibly two lines of plain text only (I think: lorem ipsum). If this loads normally, then you will find out that something specific in your code file that you are reading is twisting things. Very unlikely, but this is an obvious troubleshooting tactic.

In extreme cases, try it on another machine.

+1
source

When everything else fails, check out the stupid things ... did you find that the RichTextBox control is multi-line? Or is it set to single line mode? You might be loading the whole file correctly, but the control only displays the first line, because you said that :)

Check out RichTextBox.Multiline . It is long, but maybe?

I created a sample project with a fixed RichTextBox control, saved all the default values โ€‹โ€‹(except for Dock = DockStyle.Fill ), added a simple File-> Open menu and cut your code into the menu handler. The only change I had to make to your code was to change the second LoadFile parameter from RichTextBoxStreamType.PlainText to RichTextBoxStreamType.RichText .

A complex file (links, formatting, graphics, etc.) saved from Word is excellent for me.

Another common problem is that the control is very short. You might think that this fills your client area, but in reality it is just a narrow strip, so you see only one line. Are the size, docking and / or anchor parameters set correctly? Do you see a scroll bar for your document? Is it shorter than you expect?

+1
source

This will work:

 StreamReader sr = new StreamReader(sFileName, Encoding.Default, true); string sRtfFile = sr.ReadToEnd(); sr.Close(); rtbCombinedFile.Rtf = sRtfFile; 

sFileName is, of course, the full path to the RTF file. StreamReader is part of "System.IO".

+1
source

Just read the text file in line and set the Rtf RichTextBox property.

If you are not sure if your text contains Rtf text, you can use this class that I wrote. It inherits the original RichTextBox and has a backup if the content is not rtf or incorrectly formatted.

 public class RichTextBoxEx :RichTextBox { public new String Rtf { get { return base.Rtf; } set { try { // is this rtf? if (Regex.IsMatch(value, @"^{\\rtf1")) { base.Rtf = value; } else { base.Text = value; } } catch (ArgumentException) // happens if rtf content is corrupt { base.Text = value; } } } } 
+1
source

I think the problem is with RichTextBoxStreamType because you set it to PlainText, but want RichText to load into the richtextbox control, so why not use RichTextBoxStreamType.RichText. I tried the following code and it works correctly.

  // Create an OpenFileDialog to request a file to open. OpenFileDialog openFile1 = new OpenFileDialog(); // Initialize the OpenFileDialog to look for RTF files. openFile1.DefaultExt = "*.rtf"; openFile1.Filter = "RTF Files|*.rtf"; // Determine whether the user selected a file from the OpenFileDialog. if (openFile1.ShowDialog() == System.Windows.Forms.DialogResult.OK) { // Load the contents of the file into the RichTextBox. richTextBox1.LoadFile(openFile1.FileName, RichTextBoxStreamType.RichText); } 
0
source

Not sure if this is the same in WinForms as it is in WPF, but in WPF you need to use the FlowDocument installed in the RichTextBox Property Document. this is the code I have to read from WebStream (the same thing can be done for FileStreams

 protected static FlowDocument LoadRemoteRtf(string path) { var doc = new FlowDocument(); if (!string.IsNullOrEmpty(path)) { var range = new TextRange(doc.ContentStart, doc.ContentEnd); var downloader = new WebClient(); Stream stream = null; try { stream = downloader.OpenRead(path); range.Load(stream, DataFormats.Rtf); } catch (Exception ex) { var props = new Dictionary<string, object> {{"URL", path}}; Logging.WriteLogEntry("Failed to load remote RTF document.", ex, TraceEventType.Information, props); } finally { if (stream != null) { stream.Close(); } downloader.Dispose(); } } return doc; } MyRTB.Document = LoadRemoteRtf("http://myserver.com/docs/remote.rtf"); 
0
source

You can also try setting the "RichTextBox Modifiers" property to "public" and see if it works, and also check that the WordWrap property is set to true, that is, if the file you are reading is written on 1 line, just a long line even if it will not wrap these long lines based on the size of your RichTextBox.

I donโ€™t know if you are using it already, have you tried ReSharper?

0
source

Source: https://habr.com/ru/post/1308546/


All Articles