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

Suspend Update

3 Answers 462 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
Scott Michetti
Top achievements
Rank 1
Iron
Scott Michetti asked on 20 May 2014, 01:53 PM
Hello, does the RichTextBox have a method that will prevent the UI updating while I change the text in code? At times, some of the updates I make don't show up in the UI, and I'm thinking it's a timing or refresh issue. I'm editing the existing text. Sometimes the font won't change, sometimes the alignment won't change etc. My code looks something  like this:

radRichText.ChangeTextAlignment(Telerik.Windows.Documents.Layout.RadTextAlignment.Center);
radRichText.ToggleItalic();
radRichText.ChangeTextForeColor((Color)ViewModel.TextColor);
radRichText.Background = new SolidColorBrush((Color)ViewModel.TextBackColor);           
FontFamily fontFamily = new System.Windows.Media.FontFamily(ViewModel.Font);
 radRichTextBoxCells.ChangeFontFamily(fontFamily);


Thanks,
Scott

3 Answers, 1 is accepted

Sort by
0
Petya
Telerik team
answered on 21 May 2014, 01:52 PM
Hello Scott,

There are cases when such issues might occur, for example if you modify the document elements in a measured document directly. For example, if a document is shown in RadRichTextBox and you use the collections to obtain a Span element in it and change its Text property like in this snippet:
//DO NOT use this code
Span someSpan = (this.radRichTextBox.Document.Sections.First.Blocks.First as Paragraph).Inlines.First as Span;
someSpan.Text = "changed text";

More information about the two states of a document is available here and this article explains what to do in case you want to modify the contents of a measured document.

I'm having troubles determining the state of the document you are manipulating and the approach you are using based on your post, but please note that it is usually recommended to utilize RadRichTextBox or RadDocumentEditor for any document changes.

I hope this helps.

Regards,
Petya
Telerik
 
Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
 
0
Wai
Top achievements
Rank 1
answered on 03 Jan 2015, 07:52 AM
Hi,

I am having this exact same problem. When I update the text of a span or span box, the change does not appear on the richtextbox right away. It appears after I hit another key. I need to be able to insert some text at the location of the cursor. Could you suggest another way of doing it? The cursor has to stay at the same location. I am doing this currently.

                    DocumentPosition startPosition = this.Editor.Document.CaretPosition; 
                    var text = startPosition.GetPreviousSpanBox();
                    text.AssociatedSpan.Text += "some text";

Thanks.


0
Petya
Telerik team
answered on 07 Jan 2015, 03:01 PM
Hello Wai,

Like I've said in my previous reply, directly changing any DocumentElement in a measured document is not recommended and could cause the behavior you are observing, as well as unexpected crashes.

As far as I can tell from the code snippet in your post, you are trying to insert specific content directly after the span where the caret is located, could you confirm? Please note that a Span element does not always correspond to a single word. For example, the following: ABCDEF would be split into two separate spans because the characters have different styling.

As to retaining the caret position while executing actions, you can try an approach similar to the one from this forum thread. Create a custom annotation marker and insert it at the caret position on demand, in the case of the demo when exporting and in yours - just before editing the string. Then move the caret to the desired location in order to insert the text, e.g. this snippet inserts at the end of the current word:
this.radRichTextBox.Document.CaretPosition.MoveToNextWordStart();
this.radRichTextBox.Document.CaretPosition.MoveToPrevious();
this.radRichTextBox.Insert("some text");

I hope this is useful.

Regards,
Petya
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
RichTextBox
Asked by
Scott Michetti
Top achievements
Rank 1
Iron
Answers by
Petya
Telerik team
Wai
Top achievements
Rank 1
Share this question
or