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

how to supply input string to the content of the richTextBox and retrieving the edited content

5 Answers 509 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
Edward Pauley
Top achievements
Rank 1
Edward Pauley asked on 21 Mar 2011, 11:35 PM
Hi

I am not sure whether i am using the correct control or not. Basically, I want to use a control which will let the user realize that they type something wrong and suggest the correct answer. And radrichtextbox is the only control which i knows so far (tried spellCheck and that one doesn't work very nicely during opening multiple radwindow levels).

basically, i have Customer table which have the fields
   CustomerId, customerName, customerComment (string)
that customer collection will be bounded to the radgridview . And inside my radgridview will have an unbound column which contain a radbutton. When i click on that button , a pop-up radwindow which contains the RadRichTextBox will appear and let me modify the CustomerComment field

I read your online document and example and only see that the radrichtextbox will accept a Document type for its content (not a string type). In my case, i only have a string field. 

------->
so what i did is that , i create a Document on the fly . That document will have one paragraph. Inside that paragraph will have a span element . The way i did that is because i ONLY know that the span element will have the TEXT property which will let me set span.Text = customerComment.
And after user modify the content in the richTextBox, i will search for that span element in the RichTextBox and retrieving that TEXT property in order to update my customerComment.

I do know that my solution is kind of awkward. But i can't really find a way to use the radrichtextbox in order to let user see my customerComment ----> then modify it (having spellcheck) -----> then update that modified content to my CustomerComment field 

My Question: can you suggest a proper way to accomplish my task : "having spellcheck during type CustomerComment field" (it is not a Document type it is just a string type)
Any control is fine. There is no need to radrichtextbox. A normal textbox is fine but it must have spell check (but not forcing the user to click on a button which ask for spellCheck like the one in your demo of SpellChecker). It should spellcheck automatically.

Note; with my current solution for the RichTextBox. It does take SOME TIME in order to pop-up the comment Radwindow when the button has been clicked. I don't really know why it takes too long (around 6-7 second waiting) to let the window pop-up.

A snippet of my awkward solution
//Create Document for RadRichTextBox
           RadDocument document = new RadDocument();
           Telerik.Windows.Documents.Model.Section section = new Telerik.Windows.Documents.Model.Section();
           Telerik.Windows.Documents.Model.Paragraph paragraph = new Telerik.Windows.Documents.Model.Paragraph();
           //Setting the value of the comment to richTextBox
           Telerik.Windows.Documents.Model.Span span = new Telerik.Windows.Documents.Model.Span() { Text = selectedTBDocumentLine.CustomerComment };
            
           paragraph.Inlines.Add(span);
           
           section.Children.Add(paragraph);
           document.Sections.Clear();
           document.Sections.Add(section);
           /////
           commentScreen.commentRTB.Document = document;
           commentScreen.Closed += new EventHandler<WindowClosedEventArgs>(commentScreen_Closed);
           commentScreen.ShowDialog();
       }
 
       void commentScreen_Closed(object sender, WindowClosedEventArgs e)
       {
           (sender as RadWindow).Closed -= commentScreen_Closed;
           if (commentScreen.Header == "Customer Comment")
           {
                 // update the customerComment through the content of the radrichtextbox
               selectedTBDocumentLine.CustomerComment = (commentScreen.commentRTB.Document.Sections.First.EnumerateChildrenOfType<Telerik.Windows.Documents.Model.Paragraph>().First().Inlines.First as Telerik.Windows.Documents.Model.Span).Text;
 
               //selectedTBDocumentLine.CustomerComment = commentScreen.commentTB.Text;
               autoSave();
 
           }



thanks

5 Answers, 1 is accepted

Sort by
0
Accepted
Iva Toteva
Telerik team
answered on 23 Mar 2011, 03:07 PM
Hello Edward Pauley,

RadRichTextBox does not have a Text property because different formats for import and export of documents are supported – XAML, HTML, Docx, RTF and plain text. In order to set the contents of the document, it should be clear what format the data is in. For easier extensibility and separation of concerns, format providers that deal with the import and export of the documents are used. Here is a list of the currently available format providers and the assemblies they are included in:

  • TxtFormatProvider (plain text) – Telerik.Windows.Documents;
  • DocxFormatProvider - Telerik.Windows.Documents.FormatProviders.OpenXml;
  • RtfFormatProvider - Telerik.Windows.Documents.FormatProviders.Rtf;
  • HtmlFormatProvider - Telerik.Windows.Documents.FormatProviders.Html;
  • XamlFormatProvider - Telerik.Windows.Documents.FormatProviders.Xaml;
  • PdfFormatProvider - Telerik.Windows.Documents.FormatProviders.Pdf.
You can read more about the use of format providershere

Overall, what you need to do to get the content of the document in a specific format is to create an instance of the corresponding provider and export the document. An example is illustrated below:

public string GetXAML(RadDocument document)
{
   XamlFormatProvider provider = new XamlFormatProvider();
   return provider.Export(document);
}

To get the text stripped of all formatting, you can use TxtFormatProvider, which is included in the Telerik.Windows.Documents assembly.

Setting the content of RadRichTextBox can be done in a similar manner, if you have the content in one of these formats. For example, importing an HTML string in the document of a RadRichTextBox can be done as follows:

public void ImportHtml(string content)
{
   HtmlFormatProvider provider = new HtmlFormatProvider();
     this.radRichTextBox.Document = provider.Import(content);
}

There are some methods in the API that can be used for updating the content of the document once it it shown on screen.
Last but not least, you can data-bind the content of RadRichTextBox using the data providers, built on top of some of the format providers. You might want to review the this article about the use of data providers.
Last but not least, there is a demo of RadRichTextBox in a RadGridView in this forum thread.

Regards,
Author nickname
the Telerik team
Iva
the Telerik team
0
Amruta
Top achievements
Rank 1
answered on 27 Mar 2015, 06:20 AM
How to retrieve text of RadRichTextBox??
example : <hrml> <body> Sample Data </body></html>
Output : Sample data
How can I get just data/text entered by user and NO HTML tags????
Thanks.
0
Tanya
Telerik team
answered on 30 Mar 2015, 12:27 PM
Hi Amruta,

You could use the TxtFormatProvider to get the plain text from the RadRichTextBox control. More information and examples about how to use this feature you could find in the Import/Export article in our documentation.

Hope this helps.

Regards,
Tanya
Telerik
 

See What's Next in App Development. Register for TelerikNEXT.

 
0
Amruta
Top achievements
Rank 1
answered on 31 Mar 2015, 05:28 AM
Thanks Tanya for reply.

Below Code works if I write it in code behind file :
   ITextBasedDocumentFormatProvider exporter = new TxtFormatProvider();
            
   string text = exporter.Export(this.radtextbox.Document);

But I need to do validation in model (using MVC pattern),where I can not retrieve RadDocument object ...Can you suggest how to pass RadDocument object to model to do this validation please???

0
Tanya
Telerik team
answered on 02 Apr 2015, 05:22 PM
Hello Amruta,

You could validate the content of a RadDocument using the built-in validation. The validation should be applied over the content in the document and you do not need to pass the RadDocument instance - just pass the extracted content using the TxtFormatProvider. In order to implement your own validation rule you should inherit the ValidationRule abstract class and implement the Validate method, which determines whether the content is valid or not. I am sending you a sample project which demonstrates the validation and returns that the document is valid if its content length is between 5 and 15 symbols.

Hope this helps.

Regards,
Tanya
Telerik
 

See What's Next in App Development. Register for TelerikNEXT.

 
Tags
RichTextBox
Asked by
Edward Pauley
Top achievements
Rank 1
Answers by
Iva Toteva
Telerik team
Amruta
Top achievements
Rank 1
Tanya
Telerik team
Share this question
or