Hi
currently i'm trying to integrate Intellisense support to rad richtextbox. i was able to display word list when press Ctrl+Space. but could not replace the word. is there any one who try such a thing. i have attached a screen shot which was develop using visual studio richtextbox. can not use that code for RadRichTextBox.
7 Answers, 1 is accepted
You must have already received the answer on your e-mail, but I am going to post it here as well.
What you can do is select the current word, delete it and replace it with the word chosen by the user. You would need something like the following:
using
(DocumentPosition position =
new
DocumentPosition(richTextBox.Document.CaretPosition))
{
position.MoveToPrevious();
string
currentWord = position.GetCurrentWord();
if
(currentWord ==
"hel"
)
{
SpanLayoutBox spanBox = position.GetCurrentSpanBox();
if
(spanBox !=
null
)
{
Span currentSpan = spanBox.AssociatedSpan;
int
startIndex = currentSpan.Text.IndexOf(currentWord);
int
endIndex = startIndex + currentWord.Length;
using
(DocumentPosition start =
new
DocumentPosition(richTextBox.Document))
{
start.MoveToStartOfDocumentElement(currentSpan);
for
(
int
i = 0; i < startIndex; i++)
{
start.MoveToNext();
}
using
(DocumentPosition end =
new
DocumentPosition(start))
{
for
(
int
i = startIndex; i < endIndex; i++)
{
end.MoveToNext();
}
richTextBox.BeginUndoGroup();
richTextBox.Document.Selection.Clear();
richTextBox.Document.Selection.AddSelectionStart(start);
richTextBox.Document.Selection.AddSelectionEnd(end);
richTextBox.Delete(
true
);
richTextBox.Insert(
"hello"
);
richTextBox.EndUndoGroup(
"Intelisense"
);
}
}
}
}
}
What this code does is it gets the current caret position, the current word and the current span and from them it gets the start and the end position of the word. After that it can select the word using the two positions, delete it, and add the new word. The deletion and insertion are enclosed in an undo group, in order to appear as a single action in the history stack.
A few things to note: we move the caret position one step back, because if it is in the end of the word, the code would find the end of the document, instead of the word behind the position. You might want to extend the code to handle scenarios when the user has not yet started to type for example. Also, this algorithm would probably not perform well for cases when the word is split between spans, for example if the first half of it has a different format set, like bold.
I hope this helps.
Regards,
Anna
Progress Telerik
hi Anna
thank you for the response. i tried your solution, but it didnt work as i expect. so i came with my own solution with your recommendations.
private
void
AddTextToDocument(
string
insertText)
{
// Console.WriteLine(insertText);
using
(DocumentPosition positionInDocument =
new
DocumentPosition(
this
.Document.CaretPosition))
{
if
(
this
.Document.CaretPosition.GetCurrentSpanBox().IsFormattingSymbol)
{
positionInDocument.MoveToPrevious();
}
string
word = positionInDocument.GetCurrentWord();
using
(DocumentPosition wordEndPosition =
new
DocumentPosition(positionInDocument))
{
using
(DocumentPosition wordStartPosition =
new
DocumentPosition(positionInDocument))
{
wordEndPosition.MoveToCurrentWordEnd();
if
(!wordEndPosition.GetCurrentWord().Equals(
" "
))
{
wordStartPosition.MoveToCurrentWordStart();
}
this
.Document.Selection.AddSelectionStart(wordStartPosition);
this
.Document.Selection.AddSelectionEnd(wordEndPosition);
Console.WriteLine(
"positionInDocument.GetCurrentWord() "
+ positionInDocument.GetCurrentWord());
Console.WriteLine(
"wordStartPosition.GetCurrentWord() "
+ wordStartPosition.GetCurrentWord());
Console.WriteLine(
"wordEndPosition.GetCurrentWord() "
+ wordEndPosition.GetCurrentWord());
//this.ActiveDocumentEditor.Insert(insertText);
this
.ActiveDocumentEditor.Insert(IntellisenseWordList.IntellisenseWordsDic[insertText]);
}
}
}
this
.Focus();
AssistListBox.Visibility = Visibility.Collapsed;
}
I am happy that you've reached a better solution and thank you for sharing it with us.
Please, let us know if anything else comes up.
Regards,
Anna
Progress Telerik
Hi ,
Is there a feature like Autocomplete for text box in RichTextBox ?
I just want to add a specific set of key words to the starting line of the file ,some thing similar to Autocomplete feature in Radrich text box to do this.
Thanks in Advance
Susmitha
Hello Susmitha,
This feature is not available in RadRichTextBox. The RadSyntaxEditor has a similar feature that you can use for this: IntelliPrompts.
Let me know how this works for you.
Regards,
Dimitar
Progress Telerik
Our thoughts here at Progress are with those affected by the outbreak.
Hi Dimitar,
Thanks for the information.
But currently am using an old version of Telerik(RadSyntaxEditor is Beta in the R3 2019 release) ,so cant use this in my application.
Susmitha
Hi Susmitha,
RadRichtextBox does not have built-in functionality for this. You can use the approach suggested above to implement it.
Let me know if you have any other questions.
Regards,
Dimitar
Progress Telerik
Our thoughts here at Progress are with those affected by the outbreak.