This is a migrated thread and some comments may be shown as answers.

Read from a string.

8 Answers 162 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
Meerkat
Top achievements
Rank 1
Meerkat asked on 22 May 2010, 03:00 PM
Hello, please can someone please tell me how to load a RadRichTextBox from a string.

One of your examples somewhere shows how to open and save to a file.
However, could someone be kind enough to tell me how to read and write to a string,
which would be of more use for saving to and retrieving from a database.

The following is my attempt so far.
I have managed to read the RadRichTextBox contents, but I am afraid I cannot work out how to write it back again.
Any help would be gratefully received,
Many thanks,
Pete.

 public String EditorContents  
    {  
      get  
      {  
        IDocumentFormatProvider exporter = new XamlFormatProvider();  
        string result;  
 
        using (StringStream output = new StringStream())  
        {  
          exporter.Export(editor.Document, output);  
          result = output.Value;  
        }  
        return result;  
      }  
      set  
      {  
        editor.Document.SetText(value);  // This displays all the formatting code which is obviously not what is required
      }  
    }  
 

8 Answers, 1 is accepted

Sort by
0
Accepted
Mike
Telerik team
answered on 25 May 2010, 12:12 PM
Hello Meerkat,

You can try the following code for the property setter:

XamlFormatProvider provider = new XamlFormatProvider();
using (MemoryStream stream = new MemoryStream())
{
    StreamWriter writer = new StreamWriter(stream);
    writer.Write(value);
    writer.Flush();
    stream.Seek(0, SeekOrigin.Begin);
    editor.Document = provider.Import(stream);
}


Greetings,
Mike
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Meerkat
Top achievements
Rank 1
answered on 26 May 2010, 01:55 PM
Perfect !
Thanks Mike.
0
Ronald Evers
Top achievements
Rank 1
answered on 26 May 2010, 04:34 PM
Hello Meerkat,

Could you possibly describe what you did with the StringStream class? I tried using your method to read text from the editor but if I use a regular memorystream the stream gets closed by the exporter before I can actually read it with a streamreader and get the data into a string. Could you maybe described the StringStream class you used?
0
Meerkat
Top achievements
Rank 1
answered on 26 May 2010, 04:57 PM
Here you go Ronald.
FYI I have decided not to use the RadRichtextBox in my project.
It is just too buggy at the moment so I will have a look at the microsoft version instead..
I realise of course that it is still in beta or whatever they call it, but I cannot wait.
Regards,
Pete.

  public class StringStream : Stream  
  {  
    private MemoryStream innerStream;  
    private string value;  
 
    public StringStream()  
    {  
      this.innerStream = new MemoryStream();  
    }  
 
    public override bool CanRead  
    {  
      get { return this.innerStream.CanRead; }  
    }  
 
    public override bool CanSeek  
    {  
      get { return this.innerStream.CanSeek; }  
    }  
 
    public override bool CanWrite  
    {  
      get { return this.innerStream.CanWrite; }  
    }  
 
    public override void Flush()  
    {  
      this.innerStream.Flush();  
    }  
 
    public override long Length  
    {  
      get { return this.innerStream.Length; }  
    }  
 
    public override long Position  
    {  
      get  
      {  
        return this.innerStream.Position;  
      }  
      set  
      {  
        this.innerStream.Position = value;  
      }  
    }  
 
    public override int Read(byte[] buffer, int offset, int count)  
    {  
      return this.innerStream.Read(buffer, offset, count);  
    }  
 
    public override long Seek(long offset, SeekOrigin origin)  
    {  
      return this.innerStream.Seek(offset, origin);  
    }  
 
    public override void SetLength(long value)  
    {  
      this.innerStream.SetLength(value);  
    }  
 
    public override void Write(byte[] buffer, int offset, int count)  
    {  
      this.innerStream.Write(buffer, offset, count);  
    }  
 
    protected override void Dispose(bool disposing)  
    {  
      PrepareValue();  
      base.Dispose(disposing);  
    }  
 
    private void PrepareValue()  
    {  
      StreamReader reader = new StreamReader(this);  
      this.Seek(0, SeekOrigin.Begin);  
      this.value = reader.ReadToEnd();  
    }  
 
    public string Value  
    {  
      get  
      {  
        return this.value;  
      }  
    }  
  } 
0
Mike
Telerik team
answered on 27 May 2010, 08:23 AM
Hello guys,

We will appreciate if you submit issues you run into in the CTP version and help us deliver a more robust product in Q2!

Best wishes,
Mike
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Ronald Evers
Top achievements
Rank 1
answered on 08 Jun 2010, 08:48 AM
Thank you meerkat and sorry for the late reply. Your post helped me in the right direction and I currently got the text editor to work with minimal issues.
0
Mike
Telerik team
answered on 09 Jun 2010, 07:12 AM
Just an update guys,
In RadControls for Silverlight 4 SP2 we recently rolled out the memory stream closing issue is resolved so this StringStream is no longer needed., You'll have to dispose the stream by your side though.

Greetings,
Mike
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Meerkat
Top achievements
Rank 1
answered on 13 Jun 2010, 07:26 AM
Just to let you know that the problems I encountered before appear to have been fixed in Q2 2010 Beta version.
Many thanks.
Meerkat
Tags
RichTextBox
Asked by
Meerkat
Top achievements
Rank 1
Answers by
Mike
Telerik team
Meerkat
Top achievements
Rank 1
Ronald Evers
Top achievements
Rank 1
Share this question
or