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

How to validate there is no text in richtextbox

3 Answers 523 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
Naveen T
Top achievements
Rank 1
Naveen T asked on 31 Aug 2010, 06:40 AM
Hi,

I'm unable to validate the empty data in the rich textbox using MVVM. Here is my scenario
1.User binds some formatted text to the control,then deletes entire text in it and select save button,now how to validate that the text in the richtextbox is empty or no text.

The other issue is, I am using dataproviders example to bind the formatted text ,I am using MVVM pattern (calling documentcontentchanged  event using command) here I'm unable to get the updated content as well as binded content from it when user adds/deletes text.

Below is code i am using

<

 

 

UserControl.Resources>

 

 

 

 

 

 

<Htmlprovider:HtmlDataProvider x:Key="htmlprovider" RichTextBox="{Binding ElementName=radRichTextBox}"

 

 

 

 

 

Html="{Binding ElementName=txtblkDescformattag, Path=Text, Mode=TwoWay}"/>

 

 

 

 

 

 

 

</UserControl.Resources>
UI

 

 

 

 

<telerik:RadRichTextBox x:Name="radRichTextBox"  

 

 

<i:Interaction.Triggers>

 

 

 

 

 

<i:EventTrigger EventName="DocumentContentChanged">

 

 

 

 

 

<cmd:EventToCommand PassEventArgsToCommand="True" Command="{Binding Path=ContentChangedCommand, Mode=OneWay}" CommandParameter="{Binding Path=Docuemnt,ElementName=radRichTextBox,Mode=TwoWay}" />

 

 

 

 

 

 

 

 

 

 

 

</i:EventTrigger>

 

 

 

 

 

</i:Interaction.Triggers> </telerik:RadRichTextBox>

 

 

 

 

 

<

 

 

TextBlock Height="20" Width="10" x:Name="txtblkDescformattag" Visibility="Collapsed" Text="{Binding Path=CurrentTicket.TicketDescription,Mode=TwoWay}" />

 

 

 

Command
private RelayCommand<RadDocument> _contentChangedCommand = null;

 

 

 

 

 

 

public RelayCommand<RadDocument> ContentChangedCommand

 

{

 

 

 

 

get

 

 

 

{

 

 

if (_contentChangedCommand == null)

 

{

_contentChangedCommand =

 

 

 

new RelayCommand<RadDocument>(doc =>

 

{

 

 

 

 

try

 

 

 

{

 

 

if (!_ticketModel.IsBusy)

 

{

 

 

 

 

//string result= doc.ToString();

 

 

 

 

 

IDocumentFormatProvider exporter = new HtmlFormatProvider();

 

 

 

 

 

string result;

 

 

 

 

 

using (MemoryStream stream = new MemoryStream())

 

{

exporter.Export(doc, stream);

stream.Seek(0,

 

 

 

SeekOrigin.Begin);

 

 

 

 

 

using (StreamReader reader = new StreamReader(stream))

 

{

result = reader.ReadToEnd();

}

}

}

}

 

 

 

 

catch (Exception ex)

 

{

 

 

 

 

// notify user if there is any error

 

 

 

 

 

AppMessages.RaiseErrorMessage.Send(ex);

 

}

});

}

 

 

 

 

return _contentChangedCommand;

 

}

}


How to resolve these two issues

 

Regards,
Nav

3 Answers, 1 is accepted

Sort by
0
Alex
Telerik team
answered on 02 Sep 2010, 09:54 AM
Hello nav kum,

The easiest way to validate if the rich text box is to get the content as text using the TxtFormatProvider. Here is a short example:
ITextBasedDocumentFormatProvider exporter = new TxtFormatProvider();
string text = exporter.Export(document);
bool isEmpty = string.IsNullOrEmpty(text);

Note that the HtmlFormatProvider also implements the ITextBasedDocumentFormatProvider interface so you can use the Export method that returns string instead of exporting the document to a MemoryStream and then reading back the HTML.

About the DocumentContentChanged event - we've noticed that there is a typing mistake ("Docuemnt" instead of "Document") in the followoing line:
<cmd:EventToCommand PassEventArgsToCommand="True" Command="{Binding Path=ContentChangedCommand, Mode=OneWay}" CommandParameter="{Binding Path=Docuemnt,ElementName=radRichTextBox,Mode=TwoWay}" />

Please check if this is not the cause of your problem.
I hope this was helpful. Don't hesitate to contact us if you have more questions.

Regards,
Alex
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
Daniel Lisowski
Top achievements
Rank 1
answered on 25 Jan 2012, 06:09 PM
I am getting an error using the radRichTextBox using this example. See error below:

Object reference not set to an instance of an object.

   at Telerik.Windows.Documents.FormatProviders.Txt.TxtFormatProvider.Export(RadDocument document)

I am using the following "using statements":
using Telerik.Windows.Documents.FormatProviders;
using Telerik.Windows.Documents.FormatProviders.Txt;
using Telerik.Windows.Documents.Model;

and here is my code:
ITextBasedDocumentFormatProvider exporter = new TxtFormatProvider();
                            string text = exporter.Export(document);
                            bool isEmpty = string.IsNullOrEmpty(text);
                            if(isEmpty == false)
                            {
                                AddNotesToListExistingContact();
                            }

I believe the issue is the application does not know what "document" is. Actually ... I don't know what it is. I am only using it becuase it was in your example. My goal is to validate the textbox has content and if so then call a new method to insert that data into SharePoint. But my code breaks before the validation can be done (I think).

Please help.

0
Iva Toteva
Telerik team
answered on 26 Jan 2012, 05:26 PM
Hello Daniel,

In the latest version of the controls, you can use the IsEmpty property of RadDocument to see if the document in RadRichTextBox is empty. For instance, if your RadRichTextBox is called radRichTextBox, you can modify your code as  follows:
if(!this.radRichTextBox.Document.IsEmpty)
{
    AddNotesToListExistingContact();
}

In this way your method will be invoked only when the text box has some content.
I hope this helps.

All the best,
Iva Toteva
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

Tags
RichTextBox
Asked by
Naveen T
Top achievements
Rank 1
Answers by
Alex
Telerik team
Daniel Lisowski
Top achievements
Rank 1
Iva Toteva
Telerik team
Share this question
or